HTMLFormControlsCollection: namedItem() method
        
        
          
                Baseline
                
                  Widely available
                
                
              
        
        
        
          
                
              
                
              
                
              
        
        
      
      This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The HTMLFormControlsCollection.namedItem() method returns
the RadioNodeList or the Element in the collection whose
name or id match the specified name, or null if
no node matches.
Note that this version of namedItem() hides the one inherited from
HTMLCollection. Like that one, in JavaScript, using the array bracket
syntax with a String, like collection["value"] is
equivalent to collection.namedItem("value").
Syntax
namedItem(name)
[name]
Parameters
name- 
A string which will be used to match against the
nameoridattributes of the controls in thisHTMLFormControlsCollectionobject. 
Return value
- A 
RadioNodeListif there are multiple elements with the givennameorid, - An 
Elementif there is exactly one element with the givennameorid, or nullif no element has the givennameorid.
Note:
The returned RadioNodeList is live, meaning that its contents automatically update if elements that match the given name are added to or removed from the collection. Furthermore, it may contain non-radio input elements despite the collection's name.
Examples
>Using namedItem()
HTML
<form>
  <label for="yes">Yes</label>
  <input id="yes" name="my-radio" type="radio" />
  <label for="no">No</label>
  <input id="no" name="my-radio" type="radio" />
  <label for="maybe">Maybe</label>
  <input id="maybe" name="my-radio" type="radio" />
  <br />
  <label for="text1">Text input 1</label>
  <input id="text1" name="my-form-control" type="text" />
</form>
<div id="output"></div>
JavaScript
const form = document.querySelector("form");
const items = form.elements.namedItem("my-radio");
const output = document.querySelector("#output");
const itemIDs = Array.from(items)
  .map((item) => `"${item.id}"`)
  .join(", ");
const item2 = form.elements.namedItem("my-form-control");
output.textContent = `My items: ${itemIDs}
My single item: "${item2.id}"`;
Result
Specifications
| Specification | 
|---|
| HTML> # dom-htmlformcontrolscollection-nameditem-dev>  | 
            
Browser compatibility
Loading…
See also
HTMLCollection.namedItemthat it replaces