Reputation:
As an example lets say I have the following HTML setup.
<div class="element" data-xy-ko="value1" data-xy-ko2="value2">
<span data-xy-ko="value3">testing</span>
<ul>
<li data-xy-ko="value4" data-xy-ko2="valu5">testing</li>
<li data-xy-ko="value5">tsjbd</li>
</ul>
</div>
There are multiple data attributes. What some attributes have in common is that they begin with data-xy-ko
, data-xy-ko2
or any other combination as long as they begin with data-xy-ko
QUESTION: How can I get all the data attributes that match data-xy-ko
and run an each loop for each attribute found where I can get each attributes value, element containing it, and do other stuff for each one found.
EDIT Multiple matching attributes my be found on the same element.
Upvotes: 0
Views: 92
Reputation: 860
This isn't a direct answer as I am changing the HTML from your premise, but this is a better way to handle what you are looking to accomplish, IMO. I would do it like this:
<div class="element" data-xy-ko="value1 value2">
<span data-xy-ko="value3">testing</span>
<ul>
<li data-xy-ko="value4 value5">testing</li>
<li data-xy-ko="value5">tsjbd</li>
</ul>
</div>
Then you can say:
var myValues = $(element).attr('data-xy-ko');
var valuesArray = myValues.split(" ");
for(var i = 0; i < valuesArray.length; i++) {
switch(valuesArray[i]) {
case "value 1":
// do stuff
break;
case "value 2":
// do stuff
break;
case "value 3":
// do stuff
break;
...etc...
}
}
Upvotes: 0
Reputation: 310
You could use jQuery to select by attribute
$('[data-xy-ko]').each(function(i, e) {
var attributeValue = e.dataset.xyKo;
var elementContainingAttribute = e;
// Do other stuff for each one found
});
Upvotes: 1