I have a jQuery object which I get via code such as this:
var elements = $('#all_cats_holder div.wSelect-option-selected').detach();
The code it gets will be something such as:
<div class="wSelect-option wSelect-option-selected"><div class="wSelect-option-value" data-val="2">Some stupid cat #2</div></div>
<div class="wSelect-option wSelect-option-selected"><div class="wSelect-option-value" data-val="3">Some stupid cat #3</div></div>
<div class="wSelect-option wSelect-option-selected"><div class="wSelect-option-value" data-val="4">Some stupid cat #4</div></div>
Now I'm trying to loop through this data and get the value of data-val but I can't seem to get it to work.
My code:
var mr_val;
$.each(elements, function(index, el) {
mr_val = el.data('val');
alert(mr_val);
});
I don't even get an alert at all.
data-valattribute. You want to traverse to the child div, and then pull the attribute from that.elinside the loop is not a jQuery object, and has nodata()method