1

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.

1
  • 2
    use .attr('data-productid'); Commented Apr 20, 2016 at 6:26

3 Answers 3

3

You can get attribute value like following.

this.getAttribute('data-productid')

Or

$(this).attr('data-productid')
Sign up to request clarification or add additional context in comments.

2 Comments

Or $(this).data('productid')
Obviously. @KartikeyaKhosla
3

.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');

Comments

0

use

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.