
- HTML Home
- HTML Roadmap
- HTML Introduction
- HTML History & Evolution
- HTML Editors
- HTML Basic Tags
- HTML Elements
- HTML Attributes
- HTML Headings
- HTML Paragraphs
- HTML Fonts
- HTML Blocks
- HTML Style Sheet
- HTML Formatting
- HTML Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Frames
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Code Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Table Headers & Captions
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB & RGBA Colors
- HTML - HEX Colors
- HTML - HSL & HSLA Colors
- HTML - HSL Color Picker
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - Document Object Model (DOM)
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Character Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - DOM HTMLCollection namedItem() Method
HTML DOM HTMLCollection namedItem() method used to get the first element of collection whose id or name matches with the name mentioned in parameter of namedItem() method.
Syntax
HTMLCollection[name]; Or HTMLCollection.namedItem(name);
Parameter
It accepts one required parameter which is mentioned below.
Parameter | Description |
---|---|
name | It represents the id or name attribute of the element which you want to return. |
Return Value
It returns an element corresponding to id or name attribute mentioned in the parameter.
Examples of HTML DOM HTMLCollection 'namedItem()' Method
The following examples illustrates use of namedItem() method.
Get Content of script Element
The following example returns the content of script element with id="s3"
<!DOCTYPE html> <html lang="en"> <head> <title>HTMLCollection namedItem() Method</title> </head> <body> <script>document.getElementById("s1").innerHTML = "This is first Script."</script> <script>document.getElementById("s2").innerHTML = "This is second Script."</script> <script id="last">document.getElementById("s3").innerHTML = "This is third Script."</script> <p>Click to get content of script element with id= 's3'.</p> <button onclick="fun()">Click me</button> <p id="s1"></p> <p id="s2"></p> <p id="s3"></p> <p id="scripts"></p> <script> function fun() { let x = document.scripts.namedItem("last").text; document.getElementById("scripts").innerHTML = x; } </script> </body> </html>
Alternate way to get Content of Elements.
The following example illustrates alternate way to get the content of <h1> element with id="alt"
<!DOCTYPE html> <html lang="en"> <head> <title>HTMLCollection namedItem() Method</title> </head> <body> <h1 id="alt">I am h1 with alt id</h1> <h2>I am random text</h2> <p>Click to get content of h1 element with id= 'alt'.</p> <button onclick="fun()">Click me</button> <p id="display"></p> <script> function fun() { let x = document.getElementsByTagName("h1"); let y = x["alt"]; document.getElementById("display").innerHTML = y.innerHTML; } </script> </body> </html>
Get Content of button Element
The following example returns the content of button element with name="fun".
<!DOCTYPE html> <html lang="en"> <head> <title>HTMLCollection namedItem() Method</title> </head> <body> <p>Click on "click me" to get content of button element with name= 'fun'.</p> <button onclick="fun()">Click me</button> <button name="fun">Button 1</button> <p id="display"></p> <script> function fun() { let x = document.getElementsByTagName("button").namedItem("fun").innerHTML; //let y=x["alt"]; document.getElementById("display").innerHTML = x; } </script> </body> </html>
Supported Browsers
Method | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
namedItem() | Yes 1 | Yes 12 | Yes 1 | Yes 1 | Yes 12.1 |
html_dom.htm
Advertisements