stiffle
stiffle

Reputation: 533

Get value of a custom attribute

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

Answers (3)

Codegiant
Codegiant

Reputation: 2150

You can get the value by using attr:

$('#the_id').attr('original-title')

Upvotes: 3

Adam Wolski
Adam Wolski

Reputation: 5656

You can do it using plain JavaScript:

document.getElementById("the_id").getAttribute("original-title")

Upvotes: 74

jinsky
jinsky

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

Related Questions