Reputation: 2148
I have a dropdown list. How do I get the id of the list element which is clicked.
Here is my code
<div id='location_dropdown' class="dropdown">
<div class="dropdown-toggle" data-toggle="dropdown"></span>
<a id="selected_location"><b><font color="black">{{user}}</font> </b></a>
</div><!--end of dropdown-toggle-->
<ul class="dropdown-menu" id="locations">
<li id='1'><a>1</a></li>
<li id='2'><a>2</a></li>
<li id='3'><a>3</a></li>
<li id='4'><a>4</a></li>
</ul>
</div>
jsCode
$("#locations").click(function(e){
alert(e.target.id);
});
Upvotes: 0
Views: 844
Reputation: 15616
Assuming you want the ID of the <li>
use:
$('#locations').on('click', 'li', function(){
var id = $(this).attr('id');
});
Upvotes: 2