Get id of first div within li click
<li class="intro-item"> <a href="...">Something</a> <div id="bar1"></div> <div id="bar2"></div> <div id="bar3"></div> <div id="bar4"></div> <div id="bar5"></div> </li> I want to get the id of the first div (it's bar1 here) when li is pressed. And there is more than the above. All these lis also have an intro-item CSS class. When I click on each of them, I want to get the identifier of the first div.
The idea is this:
$('li.intro-item').click(function(){ alert($('(this) div:first').attr('id'));// here I can't apply this keyword that the problem. }); +5
4 answers
with this:
<li class="intro-item"> <a href="#">Something</a> <div id="bar1">1</div> <div id="bar2">2</div> <div id="bar3">3</div> <div id="bar4">4</div> <div id="bar5">5</div> </li> <li class="intro-item"> <a href="#">Something</a> <div id="bar21">21</div> <div id="bar22">22</div> <div id="bar23">23</div> <div id="bar24">24</div> <div id="bar25">25</div> </li> and this javascript and jQuery snippet:
$("li.intro-item").click(function () { // this is the 'clicked' DOM Element // use $(this) to wrap it into a jQuery wrapped set alert($(this).find("div:first").attr('id')); }); you have clickable li that open a warning with id li first div.
+1