Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
48
rated 0 times [  51] [ 3]  / answers: 1 / hits: 15740  / 12 Years ago, fri, december 21, 2012, 12:00:00

I have a select multiple with some options.
Each option has multiple data- attributes.
I would like to create an array that contains each of its data- values.
For example my code looks a lot like this:



<select multiple id='my_select'>
<option data-my_id='1' data-my_parent='3' data-my_name='option1'>My first option</option>
<option data-my_id='2' data-my_parent='3' data-my_name='option2'>My second option</option>
<option data-my_id='3' data-my_parent='3' data-my_name='option3'>My third option</option>
</select>


And the result I am looking for needs to be something like this:



[1,3,option1], [2,3,option2], [3,3,option3]


I have researched how to create an array with one of the data- attribute value for each of the options, giving me this [1,2,3], but I have been unsuccessful in coming up with what I need.



Thanks a lot!


More From » jquery

 Answers
9
var array = $(#my_select > option).map(function() {
return [$.map($(this).data(), function(v) {
return v;
})];
}).get();


DEMO: http://jsfiddle.net/nQ6mE/


[#81276] Thursday, December 20, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
claudiofredye

Total Points: 583
Total Questions: 101
Total Answers: 115

Location: Sao Tome and Principe
Member since Wed, Dec 29, 2021
2 Years ago
;