Jannis Jannis - 1 year ago 78
jQuery Question

jQuery how to find an element based on a data-attribute value?

I've got the following scenario:

var el = 'li';

and there are 5
's on the page each with a
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:


does not match/return anything…

The reason I can't hardcode the
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

Any ideas on what I'm missing?

Answer Source

You have to inject the value of current into an Attribute Equals selector:

$("ul").find("[data-slide='" + current + "']");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download