I've got the following scenario:
var el = 'li';
and there are 5 <li>
's on the page each with a data-slide=number
attribute (number being 1,2,3,4,5 respectively).
I now need to find the currently active slide number which is mapped to var current = $('ul').data(current);
and is updated on each slide change.
So far my tries have been unsuccessful, trying to construct the selector that would match the current slide:
$('ul').find(el+[data-slide=+current+]);
does not match/return anything…
The reason I can't hardcode the li
part is that this is a user accessible variable that can be changed to a different element if required, so it may not always be an li
.
Any ideas on what I'm missing?
When searching with [data-x=...], watch out, it doesn't work with jQuery.data(..) setter:
You can use this instead:
I improved upon psycho brm's filterByData extension to jQuery.
Where the former extension searched on a key-value pair, with this extension you can additionally search for the presence of a data attribute, irrespective of its value.
Usage:
Or the fiddle: http://jsfiddle.net/PTqmE/46/
I hope this may work better
thanks
You have to inject the value of
current
into an Attribute Equals selector:For older JavaScript environments (ES5 and earlier):
Without JQuery, ES6
I know the question is about JQuery, but readers may want a pure JS method.
I have faced the same issue while fetching elements using jQuery and data-* attribute.
so for your reference the shortest code is here:
This is my HTML Code:
This is my jQuery selector: