javascript set data attribute

HTML elements def… In the next step we assign the value of the data attribute to a variable with the name user. Type. I've tried every variation of JSON.stringifycation and escaping characters. Based on growing pressure from the webmaster community, HTML 5 has finally given in, by giving us a new "data" attribute that lets you define custom attributes in a structured way within HTML elements. We can technically define limitless data attributes to any number of elements. The getAttribute() accepts an argument which is the name of the attribute from which you want to return the value. Today we use the HTML5 Dataset API to get and set custom Data Attributes. To set a data-* attribute value, use attr. If not, elements are assigned a new attribute with indicated values and name. Dans l'exemple suivant, setAttribute() est utilisé pour définir des attributs sur un