Reputation: 13636
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
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
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