Danusorn Danusorn - 7 months ago 25
Javascript Question

jQuery get id from dropdown list not work

I need to get id from dropdown list but my code isn't work

$('.search-panel .dropdown-menu').find('a').click(function(e) {
alert(this.id);
e.preventDefault();
var param = $(this).attr("href").replace("#","");
var concept = $(this).text();
$('.search-panel span#search_concept').text(concept);
$('.input-group #search_param').val(param);

});


Here is dropdown code

<ul class="dropdown-menu" role="menu">
<li id='1'><a href="#contains">น้อยมาก</a></li>
<li id='2'><a href="#its_equal">น้อย</a></li>
<li id='3'><a href="#greather_than">ปานกลาง </a></li>
<li id='4'><a href="#less_than">นิยม </a></li>
<li id='5'><a href="#all">นิยมมาก</a></li>
</ul>

Answer

The <a> doesn't have an id, only the <li> has. Use:

alert(this.parentNode.id);

Here, this refers to <a> and not the <li>.