I have three different ul groups inside of the div. I would like to get id and data attribute value when user clicks on li element. Here is my HTML:
<div class="myMenu">
<div id="groupList1">
<fieldset>
<legend>Group 1</legend>
<ul>
<li>
<span id="gr1" data-code="GRVAL1">Test 1</span>
</li>
<li>
<span id="gr2" data-code="GRVAL2">Test 2</span>
</li>
</ul>
</fieldset>
</div>
<div id="groupList2">
<fieldset>
<legend>Group 2</legend>
<ul>
<li>
<span id="gr3" data-code="GRVAL3">Test 3</span>
</li>
<li>
<span id="gr4" data-code="GRVAL4">Test 4</span>
</li>
</ul>
</fieldset>
</div>
<div id="groupList3">
<fieldset>
<legend>Group 3</legend>
<ul>
<li>
<span id="gr5" data-code="GRVAL5">Test 5</span>
</li>
</ul>
</fieldset>
</div>
</div>
I have tried this but I didn't get the id or data attribute value:
$('.myMenu ul li').on('click', function(){
console.log($(this).prop('id'));
});
If anyone knows the way to return the id or data value please let me know. Thanks in advance.
.myMneutypo in your jQuery selector?prop("id"). Also, theidanddata-codeattributes are on aspanelement contained by theli, not on theliitself, so you should put the click handler on thespan, or use$(this).find('span')to get the element.