Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .attr() method returning undefined

<ul id="footernav">
    <li><a href="javascript:void(0);" id="chat" data-icon="custom" data-transition="none">Tools</a></li>
    <li><a href="javascript:void(0);" id="email" data-icon="custom" data-transition="none">My Ride</a></li>
    <li><a href="javascript:void(0);" id="login" data-icon="custom" data-transition="none">News</a></li>
    <li><a href="javascript:void(0);" id="skull" data-icon="custom" data-transition="none">Cool</a></li>
    <li><a href="javascript:void(0);" id="coffee" data-icon="custom" data-transition="none" class"ui-btn-active ui-state-persist">Contact</a></li>
</ul>

Now I am using this jQuery function to get the id of the li which I have clicked:

$('#footernav li').click(function(){                     
    alert($(this).attr('id'));                    
});

But it returns undefined.

like image 848
Shyantanu Avatar asked Nov 29 '22 09:11

Shyantanu


2 Answers

That's because your li don't have IDs

Use this selector instead: $('#footernav li a')

like image 122
zerkms Avatar answered Dec 10 '22 21:12

zerkms


Because you are on the li element not on the a tag.

You should listen to

$('#footernav li a').click(function(){
     alert($(this).attr('id'));

});
like image 33
mas-designs Avatar answered Dec 10 '22 21:12

mas-designs