Robert Hanson Robert Hanson - 7 months ago 28
Javascript Question

Getting Attribute From HTML Using .getAttribute() Failed

I have this code :

HTML

<select disabled class="kurir full-width" data-init-plugin="select2" name="kurir" data-productid="316">
...
</select>


jQuery

$(".kurir").change(function() {
var productid = $(this).getAttribute('data-productid');
alert (productid);
});


Why I can't get
316
from
data-productid
? What did I missed here?

Thank you.

Answer

You can get attribute value like following.

this.getAttribute('data-productid')

Or

$(this).attr('data-productid')