Reputation: 533
How can I get the value of a custom attribute using JavaScript?
Like
<strong id="the_id" original-title="I NEED THIS">
I've tried .getAttribute()
and jQuery’s .attr()
without success.
Upvotes: 51
Views: 122554
Reputation: 2150
You can get the value by using attr
:
$('#the_id').attr('original-title')
Upvotes: 3
Reputation: 5656
You can do it using plain JavaScript:
document.getElementById("the_id").getAttribute("original-title")
Upvotes: 74
Reputation: 932
Adding custom attributes makes your HTML invalid. Use custom data attributes instead:
<strong id="the_id" data-original-title="I NEED THIS">
$('#the_id').data('original-title')
https://jsbin.com/akoyut/2/edit
Upvotes: 66