I have a HTML section like this:
<li data-file="file.mp3">
<div class="pink-light playing">
Now, I want to get the “data-file” attribute of <li>.
But I have several <li> tags, and I want to get only the
attribute that has a div with “playing” class inside.
My script:
$(".playing").before($("li"),function(){
alert($(this).attr("data-file"));
});
What is wrong?
Thanks!
There are a couple of ways of doing it. If the
.playingelement will be unique, then:closestfinds the closest ancestor that matches a given selector. Since your.playingelement is a child (descendant) of theli, that’s how you can find it.Alternately, you could use the
:haspseudo-class:…but be aware that
:hasis a jQuery thing, so it can’t delegate to the selector engine.