Can I insert my attribute into an HTML tag and then access it?

For example, add the group attribute:

first <input type="radio" id="qwerty1" group="1" onClick="myclick();"><br> second <input type="radio" id="qwerty2" group="2" onClick="myclick();"><br> third <input type="radio" id="qwerty3" group="3" onClick="myclick();"><br> <script> function myclick() { alert(this.group); } </script> 
  • can. but not necessary. like onclick. - etki
  • @sitev_ru, getAttribute - to get the attribute value. Regarding the inline-record, @Etki already told you, but even if you write it like this, do you think that this in your function will be a reference to the element on which the event occurred? Try, without changing anything, to output in the console.log function (this); And by the way, why invent something if you already created a valid user attribute * data- **? - Deonis
  • Override attribute as data-group? - sitev_ru
  • @sitev_ru, yes, you can. Just calling the attribute is not pretty; he did not do anything to you;) - Deonis

2 answers 2

You can throw your own attribute ( element.setAttribute (name, value) ), but it will fail with a validation from any normal code validator. We also expect glitches and incorrect work, especially in older browsers.
Storing data for a particular attribute has always been a thorn in ... under the nails of developers.
In HTML, the fifth edition, in addition to solving a heap of other equally pressing problems, an attribute was invented that meets the standards and allows you to store arbitrarily user-defined data (but it’s not necessary to overdo it — moveton).
" Data : Yes, the savior will come (or come :)) "!
Example:

 HTML5: <span data-id='123' data-group-name='Terminators'>Group 123</span> JS: var data = document.querySelector('span').dataset; var idGroup = data.id; var nameGroup = data.groupName; 

The code will be completely valid (with the corresponding doctype, of course).

  • arbitrary attributes can be quite legally declared with the help of an additional xml namespace - etki
  • one
    It would be easy to put your attributes, then setAttribute() might not have been invented. All legal attributes would be stitched into node properties. But this is rather a crutch of the past, data-* now it is no longer just a crutch that the drunken master carved out of a tree branch (no, no, not Jackie!), But a graceful cane, polished to a shim of shadows, sandalwood, with a sword inside and aged cognac in knob. Something like that :) - user31688
  • @TheDoctor> It would be easy to put your attributes, then setAttribute () might not have been invented. .setAttribute ('class', 'ie-hover-fix'); - etki
  • @Etki, Hmm ... But what about element.className = '' or element.style.property = '' ? - user31688
  • @TheDoctor, one does not interfere. There is also element.classList. - dzhioev

You can insert your attribute this way:

<input type="radio" data-group="2">

And turn to him like this:

$('[data-group=2]')...

Read more about the attributes of data-

  • And if there is a code: <span data-group = '2'> I am here! </ Span> Something will turn out badly ... - user31688
  • everything will turn out great! What are you afraid of? - Sergey V.