HTML DOM Document getElementById() hanyar

tun hukun da kuma amfani

getElementById() hanyar ta zai yi amincewa da hukun da a samu kuma yana da id ce

id ce wanda ba a samu bagetElementById() hanyar ta zai yi amincewa da null.

getElementById() ya kasance daya daga cikin manyan hanyoyin da a fi so a HTML DOM. Kowane lokaci a kaiwa ake ɗaukar ko a samar da hukun HTML element, ake amfani da ita.

tun hukun

kowane id zai zama kai tsaye, amma:

id ce wanda ke da hukun da yanzu getElementById() tun shi na farko

kuma karɓa

haɗin gwiwar getElementsByTagName()

haɗin gwiwar getElementsByClassName()

haɗin gwiwar querySelector()

haɗin gwiwar querySelectorAll()

shafin

masanin 1

tun hukun da a samu kuma yana da id ce

document.getElementById("demo");

Try It Yourself

masanin 2

tun hukun da a samu kuma sake hukun

const myElement = document.getElementById("demo");
myElement.style.color = "red";

Try It Yourself

masanin 3

tun tsa hukun ko hukun kura

document.getElementById("demo").style.color = "red";

Try It Yourself

Syntax

document.getElementById(elementId)

Parameter

Parameter Description
elementId Required. The id Value of the Element.

Return Value

Type Description
Object

Elements with the Specified id.

If Not Found, It Will Return null.

Technical Details

getElementById() This Method Is an Important and Commonly Used Method, Because It Provides a Convenient Way to Get the Element Object Representing the Specified Document Element.

This Method Will Retrieve the Value of the id Attribute As elementId And Returns It. If No Such Element Is Found, It Will Return null. The Value of the id Attribute Is Unique in the Document, If This Method Finds Multiple Element Nodes with the Specified elementId The Element Node, It Will Randomly Return One Such Element Node, or Return null.

Note:The Name of This Method Starts With Id End, Not ID! Do Not Spell It Wrong!

In HTML Documents, This Method Always Retrieves the Attribute with the Specified id. Please Use HTMLDocument.getElementByName() Methods, to Find HTML Elements Based on the Value of Their name Attribute.

In XML Documents, This Method Is Used to Find Any Attribute of Type id, Regardless of the Name of the Attribute. If the Type of XML Attribute Is Unknown (Such as When the XML Parser Ignores or Cannot Locate the DTD of the Document), This Method Always Returns null. In Client-Side JavaScript, This Method Is Not Often Used with XML Documents. In Fact,getElementById() The Method Was Initially Defined as a Member of the HTMLDocument Interface, But It Was Later Moved into the Document Interface in the Later 2nd Level DOM.

Browser Support

document.getElementById() It Is DOM Level 2 (2001) Feature.

All Browsers Support It:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support 9-11 Support Support Support Support

Related Pages

CSS Tutorial:CSS ɗanin

CSS Reference Manual:CSS #id ɗanin

HTML DOM Reference Manual:HTML DOM id Attribute

HTML DOM Reference Manual:Object Style HTML DOM