Is there a way to store additional information in the HTML tag?

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

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

+3


source to share


3 answers


You can create your own using the new data-*

custom data attributes (see w3c ). What comes in *

is up to you (if it's really HTML):



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

      

+6


source


See data-attributes

for example here you can find more information.

Example from linked page:



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

      

+2


source


HTML5 introduced an attribute data-

just for this. So if you wanted to keep the button number you would name it data-callNum

or something similar.

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

As a warning with JS, you cannot just use normal .

to access the element due to -

, which will be interpreted as a minus operation. So instead button.data-attr

you need to do button.getAttribute('data-attr')

.

0


source







All Articles