HTML Custom Attributes

7

I am developing a system for managing public tenders and I need it to be compatible with as many devices as possible.

In some situations it was necessary to use custom attributes, for example:

<span qtd='20'>Alguma coisa...</span>

In this case, I get the value of qtd through Jquery.attr() and use it as needed, it works fine in most modern browsers, but the question is:
Is it a good practice? What problems can I have in older browsers?

    
asked by anonymous 17.03.2017 / 18:08

1 answer

11

Not good practice.

To combat this HTML5 has brought us a way to achieve what you want (custom attributes) are called data-* , where you can data-blahblah and save yourself problems that might arise, in which case you could do :

<span data-qtd='20'>Alguma coisa...</span>

And to get the attribute with jQuery is just:

$('span').data('qtd'); // 20

However, there are some things to keep in mind like this :

If you make data-minha-qtd the DOM converts this to:

  

MyQtd

And you could access it like this:

$('span').data('minhaQtd');

A note from someone who knows more than me

That points to this

Regarding the compatibility of data attributes and older browsers, I suppose there is no problem, nor in IE6 , tests that were not performed by me but I believe in that colleague of SO EN.

    
17.03.2017 / 18:16