Michael
Michael

Reputation: 13636

Is there any way to save additional information in HTML tag?

I created multiple buttons, for each created button I want to save some information in an attribute so that I can use it when the button is clicked.

Is there any attribute in HTML button that I can store information to use it at some point?

Upvotes: 3

Views: 1514

Answers (3)

Patrick Hofman
Patrick Hofman

Reputation: 157126

You can create your own using the new data-* custom data attributes (see w3c specs). What comes at the * is up to you (as long as it is valid HTML of course):

<button id="x123"
        data-some-attr="I like this"
        data-what-about-this="I like it too"
/>

Upvotes: 6

David
David

Reputation: 4883

HTML5 introduced the data- attribute just for this. So if you were to store a button number, you would call it data-callNum or something similar.

You can read more up on it here: http://www.w3schools.com/tags/att_global_data.asp

As a warning with JS, you can't just use the usual . to access the member, due to the - which will be interpreted as a minus operation. So instead of button.data-attr you have to do button.getAttribute('data-attr').

Upvotes: 0

pavel
pavel

Reputation: 27092

See the data-attributes, for example here you can find more info.

Example from linked page:

<li class="user" data-name="John Resig" data-city="Boston" data-lang="js" data-food="Bacon">...</li>

Upvotes: 2

Related Questions