tElementById() - Web APIs | MDN
Maybe your like
- Skip to main content
- Skip to search
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
- Learn more
- See full compatibility
- Report feedback
The getElementById() method of the Document interface returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quickly.
If you need to get access to an element which doesn't have an ID, you can use querySelector() to find the element using any selector.
Note: IDs should be unique inside a document. If two or more elements in a document have the same ID, this method returns the first element found.
In this article
- Syntax
- Examples
- Usage notes
- Specifications
- Browser compatibility
- See also
Syntax
jsgetElementById(id)Note: The capitalization of "Id" in the name of this method must be correct for the code to function; getElementByID() is not valid and will not work, however natural it may seem.
Parameters
idThe ID of the element to locate. The ID is a case-sensitive string which is unique within the document; only one element should have any given ID.
Return value
An Element object describing the DOM element object matching the specified ID, or null if no matching element was found in the document.
Examples
HTML
html<p id="para">Some text here</p> <button>blue</button> <button>red</button>JavaScript
jsfunction changeColor(newColor) { const elem = document.getElementById("para"); elem.style.color = newColor; } document.querySelectorAll("button").forEach((button) => { button.addEventListener("click", (event) => { changeColor(event.target.textContent.toLowerCase()); }); });Result
Usage notes
Unlike some other element-lookup methods such as Document.querySelector() and Document.querySelectorAll(), getElementById() is only available as a method of the global document object, and not available as a method on all element objects in the DOM. Because ID values must be unique throughout the entire document, there is no need for "local" versions of the function.
Example
html<div id="parent-id"> <p>hello word1</p> <p id="test1">hello word2</p> <p>hello word3</p> <p>hello word4</p> </div> jsconst parentDOM = document.getElementById("parent-id"); const test1 = parentDOM.getElementById("test1");If there is no element with the given id, this function returns null. Note that the id parameter is case-sensitive, so document.getElementById("Main") will return null instead of the element <div id="main"> because "M" and "m" are different for the purposes of this method.
Elements not in the document are not searched by getElementById(). When creating an element and assigning it an ID, you have to insert the element into the document tree with Node.insertBefore() or a similar method before you can access it with getElementById():
jsconst element = document.createElement("div"); element.id = "test"; const el = document.getElementById("test"); // el will be null!In non-HTML documents, the DOM implementation must have information on which attributes are of type ID. Attributes with the name "id" are not of type ID unless so defined in the document's DTD. The id attribute is defined to be of ID type in the common cases of XHTML, XUL, and others. Implementations that do not know whether attributes are of type ID or not are expected to return null.
Specifications
| Specification |
|---|
| DOM# ref-for-dom-nonelementparentnode-getelementbyid② |
Browser compatibility
See also
- Document reference for other methods and properties you can use to get references to elements in the document.
- Document.querySelector() for selectors via queries like 'div.myclass'
- Document.evaluate() - has a utility method for selecting by xml:id in XML documents
Help improve MDN
Was this page helpful to you? Yes No Learn how to contributeThis page was last modified on Jun 3, 2025 by MDN contributors.
View this page on GitHub • Report a problem with this content Filter sidebar- Document Object Model (DOM)
- Document
- Constructor
- Document()
- Instance properties
- activeElement
- activeViewTransition
- adoptedStyleSheets
- alinkColor Deprecated
- all Deprecated
- anchors Deprecated
- applets Deprecated
- bgColor Deprecated
- body
- characterSet
- childElementCount
- children
- compatMode
- contentType
- cookie
- currentScript
- customElementRegistry
- defaultView
- designMode
- dir
- doctype
- documentElement
- documentURI
- domain Deprecated
- embeds
- featurePolicy Experimental
- fgColor Deprecated
- firstElementChild
- fonts
- forms
- fragmentDirective
- fullscreen Deprecated
- fullscreenElement
- fullscreenEnabled
- head
- hidden
- images
- implementation
- lastElementChild
- lastModified
- lastStyleSheetSet Non-standard Deprecated
- linkColor Deprecated
- links
- location
- pictureInPictureElement
- pictureInPictureEnabled
- plugins
- pointerLockElement
- preferredStyleSheetSet Non-standard Deprecated
- prerendering Experimental
- readyState
- referrer
- rootElement Deprecated
- scripts
- scrollingElement
- selectedStyleSheetSet Non-standard Deprecated
- styleSheets
- styleSheetSets Non-standard Deprecated
- timeline
- title
- URL
- visibilityState
- vlinkColor Deprecated
- xmlEncoding Deprecated
- xmlVersion Deprecated
- Static methods
- parseHTML() Experimental
- parseHTMLUnsafe()
- Instance methods
- adoptNode()
- append()
- ariaNotify() Experimental
- browsingTopics() Non-standard Deprecated
- caretPositionFromPoint()
- caretRangeFromPoint() Non-standard
- clear() Deprecated
- close()
- createAttribute()
- createAttributeNS()
- createCDATASection()
- createComment()
- createDocumentFragment()
- createElement()
- createElementNS()
- createEvent() Deprecated
- createExpression()
- createNodeIterator()
- createNSResolver() Deprecated
- createProcessingInstruction()
- createRange()
- createTextNode()
- createTouch() Non-standard Deprecated
- createTouchList() Non-standard Deprecated
- createTreeWalker()
- elementFromPoint()
- elementsFromPoint()
- enableStyleSheetsForSet() Non-standard Deprecated
- evaluate()
- execCommand() Deprecated
- exitFullscreen()
- exitPictureInPicture()
- exitPointerLock()
- getAnimations()
- getElementById()
- getElementsByClassName()
- getElementsByName()
- getElementsByTagName()
- getElementsByTagNameNS()
- getSelection()
- hasFocus()
- hasPrivateToken() Experimental
- hasRedemptionRecord() Experimental
- hasStorageAccess()
- hasUnpartitionedCookieAccess()
- importNode()
- moveBefore()
- mozSetImageElement() Non-standard
- open()
- prepend()
- queryCommandEnabled() Non-standard Deprecated
- queryCommandState() Non-standard Deprecated
- queryCommandSupported() Non-standard Deprecated
- querySelector()
- querySelectorAll()
- releaseCapture() Non-standard
- replaceChildren()
- requestStorageAccess()
- requestStorageAccessFor() Experimental
- startViewTransition()
- write() Deprecated
- writeln() Deprecated
- Events
- afterscriptexecute Non-standard Deprecated
- beforescriptexecute Non-standard Deprecated
- DOMContentLoaded
- fullscreenchange
- fullscreenerror
- pointerlockchange
- pointerlockerror
- prerenderingchange Experimental
- readystatechange
- scroll
- scrollend
- scrollsnapchange Experimental
- scrollsnapchanging Experimental
- securitypolicyviolation
- selectionchange
- visibilitychange
- Inheritance
- Node
- EventTarget
- Related pages for DOM
- AbortController
- AbortSignal
- AbstractRange
- Attr
- CDATASection
- CharacterData
- Comment
- CustomEvent
- DOMError Deprecated
- DOMException
- DOMImplementation
- DOMParser
- DOMTokenList
- DocumentFragment
- DocumentType
- Element
- Event
- EventTarget
- HTMLCollection
- MutationObserver
- MutationRecord
- NamedNodeMap
- Node
- NodeIterator
- NodeList
- ProcessingInstruction
- QuotaExceededError Experimental
- Range
- ShadowRoot
- StaticRange
- Text
- TreeWalker
- XMLDocument
- XPathEvaluator
- XPathExpression
- XPathResult
- XSLTProcessor
- Guides
- Anatomy of the DOM
- Attribute reflection
- Selection and traversal on the DOM tree
- Building and updating the DOM tree
- Working with events
Tag » What Is Getelementbyid In Javascript
-
How "getElementByID" Works In JavaScript? - Tutorialspoint
-
HTML DOM Document GetElementById() Method - W3Schools
-
JavaScript - GetElementbyId 사용법 - 기억보다 기록을
-
What Is The GetElementByID Method() In JavaScript?
-
What Is tElementById() In JavaScript?
-
JavaScript - tElementById() Method - Javatpoint
-
How GetElementByID Works In JavaScript ? - GeeksforGeeks
-
JavaScript GetElementById() - Selecting An Element By Id
-
Javascript GetElementById - W3schools.blog
-
JavaScript GetElementById() - EduCBA
-
tElementById In Javascript - Lesson 1 - YouTube
-
[JavaScript] tElementById - 블로그 - 네이버
-
[JavaScript] JS GetElementById() 이용한 아이디 요소 객체 선택
-
[Javascript] tElementById() - 하늘과 나의 IT 이야기