Saint Robson
Saint Robson

Reputation: 5525

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.

Upvotes: 1

Views: 88

Answers (3)

KarayelTasarim
KarayelTasarim

Reputation: 1

use

var productid = $(this).data('productid');

Upvotes: 0

krishna
krishna

Reputation: 518

.getAttribute is the javascript method we can't use it is jquery object method.

so you can use it below syntax- this.getAttribute('data-productid');

Upvotes: 3

Ibrahim Khan
Ibrahim Khan

Reputation: 20740

You can get attribute value like following.

this.getAttribute('data-productid')

Or

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

Upvotes: 3

Related Questions