Best Practice: Switch Group Access

Look for the best, cross-browser compatible standards, a transition-compatible way to access a group of radio buttons in the DOM (this is closely related to my other most recent post ...), without using any external libraries.

<input type="radio" value="1" name="myRadios" />One<br /> <input type="radio" value="2" name="myRadios" />Two<br /> 

I read conflicting information about getElementsByName () which seems like the right way to do this, but I don't understand if this is a standard compatible solution compatible. Perhaps there is a better way?

+4
source share
2 answers

I'm not quite happy with this solution, but finally I followed this up in the DOC Gecko documentation.

name gets or sets the name attribute of the DOM object, it applies only to the following elements: anchor, applet, form, frame, iframe, image, input, map, meta, object, option, param, select and textarea.

 HTMLElement.name = string; var elName = HTMLElement.name; var fControl = HTMLFormElement.elementName; var controlCollection = HTMLFormElement.elements.elementName; 

More details here: https://developer.mozilla.org/en/DOM/element.name

So, I suppose this is a standardized way to continue (although best practice may not exist ...)

 document.getElementById('myForm').elements.myRadios 
+2
source

I think it's best to go ahead:

 form.elements.myRadios 

vs

 form.myRadios 

The reason is that according to the last spec , elements.someName will return a RadioNodeList object (if multiple elements have the same name) that contains a value attribute indicating the value of the marked element.

form.myradios , on the other hand, will return a NodeList object, which is just an ordered set of elements and has no value property.

Searching for marked items on the radio and checkboxes has traditionally been a very painful exercise.

However, I'm not sure how we can access all the checked items for checkboxes. Could not find this in the spec. Hope this is not lost.

0
source

Source: https://habr.com/ru/post/1303998/


All Articles