Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to get value of data attribute using jQuery

Tags:

html

jquery

Here it's my html code

<select class="changeOption">
 {% for dBoy in dBoyList %}
    <option value="{{dBoy.id}}" data-locid='{{dboy.location.id}}'
 {% endfor %} 
</select>

When i'm select any option from dropdown list it shows undefine

JQuery code

$( ".changeOption" ).change(function() {
    var locId = $(this).data("locid")
    alert("id:  "+locId);
});
like image 247
NIKHIL RANE Avatar asked Feb 04 '15 15:02

NIKHIL RANE


1 Answers

this refers to context of select element. you need to find selected option in it and then get the attribute:

$(".changeOption").change(function() {
  var locId = $(this).find(':selected').data("locid")
  alert("id:  "+locId);
});
like image 151
Milind Anantwar Avatar answered Oct 18 '22 21:10

Milind Anantwar