Question :
I’m trying to find a li
with a certain class ( selected
), however, I have not been able to find id
of it.
Here is the code
var slide_act = $(".bull").find("selected").attr("id");
console.log(slide_act);
The undefined
appears on the console whenever I run the click
function. Any explanation?
Complementary Code
<ul class="bullets">
<li class="bull selected" id="slide1"></li>
<li class="bull" id="slide2"></li>
<li class="bull" id="slide3"></li>
</ul>
Answer :
You can simply get the “id” attribute of the element that has both the “bull” class and the “selected” class:
var slide_act = $(".bull.selected").attr("id");
See example working:
var slide_act = $(".bull.selected").attr("id");
console.log(slide_act);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><ulclass="bullets">
<li class="bull selected" id="slide1"></li>
<li class="bull" id="slide2"></li>
<li class="bull" id="slide3"></li>
</ul>