Create parameters in div to use in Javascript

Asked

Viewed 220 times

1

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

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

1 answer

2


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

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

In HTML that is called attributes data-* and there is an API to use this in Javascript which is .dataset, and that returns a "map of Domstring", object-like.

An example would be:

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

  • Thank you very much, I guess I wasn’t looking right, but vlw anyway!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.