Create parameters in div to use in Javascript

1

I would like to know if I can create custom parameters within a div and if it is recommended by W3C, I would like to create some parameters for a tag, but I did not find any references on the internet, an example below with the parameter < strong> pineapple

<div class="teste" abacaxi="false"></div>
    
asked by anonymous 29.04.2017 / 20:43

1 answer

2

For this, practice is data- fields. Much like what you did, but it would be

<div class="teste" data-abacaxi="false"></div>

In HTML this attributes data-* and there is an API to use this in JavaScript that is .dataset , and that returns a "map of DOMString", similar to an object.

An example would be:

var div = document.querySelector('div');
var valor = div.dataset.abacaxi;
console.log(valor);
<div class="teste" data-abacaxi="false"></div>
    
29.04.2017 / 20:46