Is it correct to name html tag attributes arbitrarily?

6

I'd like to know about creating and manipulating attributes in html that are created . We have the attributes already known as id , class , etc. that we put in the tags so we can manipulate them. I went in search of other alternatives of names, that could attend to me in that end. I saw the " date - " that after the same one can put the name you want (correct me if it is wrong here ) and do such manipulation through that attribute that came with or html5. After using the "data-", I checked if I could create an attribute in the tag with the name I wanted. What I did was this:

<a href="#" data-grupo="13" numberlink="1" class="link" id="um">Link 1</a>

When I went into the element inspector and selected the element through the attribute:

$("a[data-grupo=13]");

He found the element. With this I understand that I can create these tag attributes according to my desire. But I do not know if this is a good practice, because there may be attribute differences for attributes such as the example class and id, in which it can only belong to one element and one to more than one element.

I could not get answers to these questions, I did not find what I wanted here:

Meanings of data-value attributes, data-title, date -... in HTML

Use Attributes unary HTML tags with assigned value

dir, html or css attribute

Can I name html tag attributes according to my wishes without generating any problems (in manipulation or any other use of tags or attributes)?

    
asked by anonymous 04.02.2016 / 15:04

1 answer

2

Yes and no: you can not get added imaginary elements such as meunovoatributo="xxxx" since HTML considers only a series of predefined attributes valid in the specification. However, data- has been added to HTML5 exactly so you can create attributes as you wish, so you can play your data-qualquercoisa at will through HTML.

The only problem you could encounter is a collision of a data- its attribute with that of some external library, after all nothing prevents the awesomechartlib.js library you're using to make amazing graphics in your app put data-chart in all tags, and you may be using this data-chart for different purposes.

As for what an attribute means to be "valid" in a tag, this is referring to the HTML specification. As HTML is actually a subset of XML the formal validation of it occurs through a DTD (which is done based on in this specification ) where you define what this "valid" structure is, . As pointed out in @Haxz comment this ends up being a matter of correct semantics, in practice nothing prevents you from adding incorrect random attributes and this would hardly bring you problems, your HTML will probably render naturally in all browsers and uses with javascript / jQuery or other external element selectors, HTML / XML parsers in any languages for example, will work as if everything is correct.

    
04.02.2016 / 15:30