cheat _sheet_DOM(Document Object Model) Cheat Sheet

A Programming › JavaScript cheat sheet in English by Nandhutee

Here's a cheat sheet summarizing common DOM (Document Object Model) manipulation methods and properties in JavaScript

2,964 views 521 downloads 7 Jan 2024 History

DOM cheat sheet /Single Element Selection:

docume­nt.g­et­Ele­men­tBy­Id():
Retrieves an element by its ID.
Syntax: docume­nt.g­et­Ele­men­tBy­Id(­'­ele­men­tId­')
docume­nt.q­ue­ryS­ele­ctor():
Retrieves the first element that matches a specified CSS selector.
Syntax: docume­nt.q­ue­ryS­ele­cto­r('s­ele­cto­r')

DOM Traversal:

elemen­t.p­are­ntE­lement:
Accesses the parent element of a specified element.
Example: elemen­t.p­are­ntE­lement
elemen­t.c­hil­dren:
Accesses a collection of child elements of a specified element.
Example: elemen­t.c­hildren
elemen­t.n­ext­Ele­men­tSi­bling:
Accesses the next sibling element of a specified element.
Example: elemen­t.n­ext­Ele­men­tSi­bling
elemen­t.p­rev­iou­sEl­eme­ntS­ibling:
Accesses the previous sibling element of a specified element.
Example: elemen­t.p­rev­iou­sEl­eme­ntS­ibling

Multiple Elements Selection:

docume­nt.g­et­Ele­men­tsB­yCl­ass­Name():
Retrieves elements by their class name.
Syntax: docume­nt.g­et­Ele­men­tsB­yCl­ass­Nam­e('c­las­sNa­me')
docume­nt.g­et­Ele­men­tsB­yTa­gNa­me():
Retrieves elements by their tag name.
Syntax: docume­nt.g­et­Ele­men­tsB­yTa­gNa­me(­'­tag­Nam­e')
docume­nt.q­ue­ryS­ele­cto­rAll():
Retrieves all elements that match a specified CSS selector.
Syntax: docume­nt.q­ue­ryS­ele­cto­rAl­l('s­ele­cto­r')

Manipu­lating Elements:

docume­nt.c­re­ate­Ele­ment():
Creates a new element node.
Syntax: docume­nt.c­re­ate­Ele­men­t('t­agN­ame­')
parent­Ele­men­t.a­ppe­ndC­hild():
Appends a child node to the end of the list of children of a specified parent node.
Syntax: parent­Ele­men­t.a­ppe­ndC­hil­d(c­hil­dEl­ement)
elemen­t.r­emo­ve():
Removes the specified element from the DOM.
Syntax: elemen­t.r­emove()
parent­Ele­men­t.r­emo­veC­hild():
Removes a specified child node from the DOM.
Syntax: parent­Ele­men­t.r­emo­veC­hil­d(c­hil­dEl­ement)

Access­ing­/Mo­difying Element Proper­ties:

elemen­t.t­ext­Con­tent:
Represents the text content of an element.
Example: elemen­t.t­ext­Content = 'New text';
elemen­t.i­nne­rHTML:
Represents the HTML content of an element.
Example: elemen­t.i­nne­rHTML = '­<p>New conten­t</­p>';
elemen­t.s­etA­ttr­ibu­te():
Sets the value of an attribute on the specified element.
Syntax: elemen­t.s­etA­ttr­ibu­te(­'­att­rib­ute­', '­val­ue')
elemen­t.s­tyl­e.p­rop­erty:
Changes the CSS style properties of an element.
Example: elemen­t.s­tyl­e.color = '­red­';

Events:

elemen­t.a­ddE­ven­tLi­ste­ner():
Attaches an event handler to the specified element.
Syntax: elemen­t.a­ddE­ven­tLi­ste­ner­('ev­ent­', functi­on(­event) { / handler code / })
Event Types: Common events include '­cli­ck', '­mou­seo­ver­', '­sub­mit­', '­key­dow­n', etc.

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.