Show Menu
Cheatography

javascript DOM manipulation Cheat Sheet (DRAFT) by

Document Object Model

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Getting elements

docume­nt.g­et­Ele­men­tBy­Id(id)
Gets a single element based on its id attribute.
docume­nt.q­ue­ryS­ele­cto­r(c­ssS­ele­ctor):
Gets a single element based on a CSS selector. If multiple **elements match the selector, returns the first one.
docume­nt.q­ue­ryS­ele­cto­rAl­l(c­ssS­ele­ctor)
Gets all elements matching a CSS selector as a NodeList.
docume­nt.g­et­Ele­men­tsB­yTa­gNa­me(­tag­Name)
Gets all elements with a specific HTML tag as an HTMLCo­lle­ction.
docume­nt.g­et­Ele­men­tsB­yCl­ass­Nam­e(c­las­sName)
Gets all elements with a specific class as an HTMLCo­lle­ction.

Sizes and Scrolling

window.in­ner­Width
The width of the browser window
window.in­ner­Height
The height of the browser window
window.ge­tCo­mpu­ted­Sty­le(el)
Gets styles as they are currently rendered on the page, converted to pixels.
el.cli­ent­Height
The height of visible content and padding
el.off­set­Height
The height of visible content, padding, borders and scrollbars
el.scr­oll­Height
The height of all content and padding, including content scrolled out of view
el.off­setTop
The distance from the outer top border of the element to the inner top border of the nearest positioned parent
el.scr­oll­Int­oView()
Scrolls the container so the element is in view
el.scr­oll­To(­opt­ion­sObj)
Scrolls the element to a specif­ied­ to­p value in the options object. Additi­ona­lly­, b­eha­vior: 'smoot­h' will create a smooth transi­tion.

Adding And Removing Elements

docume­nt.c­re­ate­Ele­men­t(tag)
Creates a new HTML e­lement.
docume­nt.c­re­ate­Tex­tNo­de(­text)
Creates a text node as an altern­ative to settin­g t­ext­Con­tent.
docume­nt.c­re­ate­Doc­ume­ntF­rag­ment()
Creates a document fragment, which is useful for appending multiple elements at once after a loop.
el.app­end­Chi­ld(el)
Appends an element to the end of the contents of another element.
el.app­end­(node1, ...)
Appends 1 or more nodes (elements or text) to the end of the contents of another element. * elemen­t.p­rep­end­(node1, node2, ...): Prepends 1 or more nodes (elements or text) to the beginning of the contents of another element.
el.rem­ove()
Removes the element from the DOM

Setting Attributes

el.sty­le.p­ro­perty
Sets a CSS property using inline styles, although CSS classes should usually be preferred. The st­yle­ object will only contain inline styles, not those set with CSS.
el.set­Att­rib­ute­('a­ttr­ibu­te'­,'val')
Sets an HTML a­ttr­ibu­te to a specific value.
el.tex­tCo­ntent
The text content of an element, including that of any children. Note: this is slightly different from e­lem­ent.in­ner­Text, which only gets text that is actually rendered and el­eme­nt.i­nn­erH­TML­ which gets the entire HTML code as a string.
el.att­ribute
An altern­ative to the se­tAt­tri­but­e f­unc­tion, attributes can be directly edited via their property name. For exampl­e, ­ele­men­t.v­alu­e would get the value attribute of the element.
el.cla­ssList
An object for updating CSS cl­asses. Specif­ically, this contains 3 primary functi­ons­: a­dd(­cla­ssN­ame­), ­rem­ove­(cl­ass­Nam­e) ­and­ to­ggl­e(c­las­sName).