python-coder
python-coder

Reputation: 2148

How to get the id of dropdown list clicked

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

Answers (2)

Sid M
Sid M

Reputation: 4354

$("#locations li").click(function() {
    var id=this.id; 
});

Upvotes: 0

Tom Walters
Tom Walters

Reputation: 15616

Assuming you want the ID of the <li> use:

$('#locations').on('click', 'li', function(){
    var id = $(this).attr('id');
});

Upvotes: 2

Related Questions