Is it correct to name html tag attributes arbitrarily?

Asked

Viewed 474 times

6

I would like to know about creating and manipulating attributes in html that are created. We have the attributes already known as the 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 serve me in this order. I saw the "date-" that after the same one can put the name you want (correct me if I’m wrong here) and make such manipulation through that attribute that came with Html5. After using "date-", I checked if I could create an attribute in the tag with the name I wanted. What I did was the following:

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

When I went to the element inspecter and selected the element through the attribute:

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

He found the element. With that I understood that I can create these tag attributes according to my wish. But I don’t know if this is good practice, because there may be differences of attributes for attributes like the example of class and id, in which this can only belong to one element and the one to more than one element.

I couldn’t get an answer to these questions, I couldn’t find what I wanted here in the:

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

Use unary HTML tags attributes with assigned value

dir, html or css attribute

I can name attributes of html tags according to my will without this generating me problems (in manipulation or any other use of tags or attributes)?

  • 1

    Your questions are usually exemplary.

  • Do you have a problem with them being like this? if I have I can try to change the way of asking :) I always try to test first, then I look here in only and on the net, when I find nothing that satisfies my doubt I ask here.

  • 3

    Of course, it shows how the others are worse :) Of course you can always improve, but yours can’t get much better, they do everything a good question should ask. Congratulations!

  • Related: http://meta.pt.stackoverflow.com/questions/4829/eu-n%C3%a3o-sei-perguntar-e-voc%C3%aa? cb=1 :D

  • vlw bigown! obg!

1 answer

2


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

The only problem you could find is collision of an attribute data- yours with that of some external library, after all nothing prevents the library awesomechartlib.js that you are using to make amazing graphics in your app put data-chart all tags, and you may be using this one yourself data-chart for different purposes.

As to what an attribute means to be "valid" in a tag, this refers to the HTML specification. Since HTML is actually a subset (succonjunct) of XML the formal validation of it takes place through a DTD (which is made on the basis of in this specification) where what is defined is this "valid" structure. As pointed out in @Haxz’s comment this turns out to be a matter of correct semantics, in practice nothing prevents you from adding incorrect random attributes and it would hardly cause 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.

  • 1

    Brunorb, what does it mean to be "considered valid"? in my question I was able to recover the attribute created via jquery. In this case it would not be valid?

  • 3

    I think it’s more of a "semantically valid"

Browser other questions tagged

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