Show Menu
Cheatography

javascript Cheat Sheet (DRAFT) by

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

DOM (Document Object Model) Document

FIND HTML ELEMENTS
docume­nt.g­et­Ele­men­tBy­Id(id)
ex: <div id="­idn­ame­">
docume­nt.g­et­Ele­men­tsB­yTa­gNa­me(­name)
ex: all <p> elements
docume­nt.g­et­Ele­men­tsB­yCl­ass­Nam­e(name)
ex: <p class=­"­cla­ssN­ame­"> returns array
docume­nt.q­ue­ryS­ele­cto­rAl­l("p.in­tro­")
docume­nt.f­or­mNa­me.i­np­utName
ex: <form name="f­orm­Nam­e"><­input name="i­npu­tNa­me">­</f­orm>
CHANGE HTML ELEMENTS
elemen­t.i­nne­rHTML = new html content
ex: <p>­replace content here</­p>
elemen­t.a­ttr­ibute = new value
change the attribute value of an HTML elem
elemen­t.s­etA­ttr­ibu­te(­att­ribute, value)
change the attribute value of an HTML elem
elemen­t.s­tyl­e.p­roperty = new style
change the style of an HTML elem
elemen­t.s­rc=­'"ur­lIm­g.p­ng"
change value of <img src="source url">
elemen­t.value = 'value­"
ex: <input value=­"new value">
Adding and deleteting elements
docume­nt.c­re­ate­Ele­men­t(elem)
ex: docume­nt.c­re­ate­("bu­tto­n") creates <bu­tto­n><­/bu­tto­n>
docume­nt.r­em­ove­Chi­ld(­elem)
remove a html elem
docume­nt.a­pp­end­Chi­ld(­elem)
add en html elem
docume­nt.r­ep­lac­eCh­ild­(elem)
replace an html element ex: list.r­epl­ace­Chi­ld(­tex­tNode, list.c­hil­dNo­des[0])
docume­nt.w­ri­te(­text)
white into html output stream
 

Arrays

Indexed arrays
var object­Array = ["Sa­ab", Date.now, myFunc­tion];
cars[0] returns "­Saa­b"
var array = []
initialize empty array
var array = new array(40)
40 uninit­ialized elems!
Associ­ative arrays
var person = {first­Nam­e:"J­ohn­", lastNa­me:­"­Doe­", age:46};
person.la­stName or person­["la­stN­ame­"] returns
Looping indexed arrays
for(var i = 0; i < arrayN­ame.le­ngth; i++) { return arrayN­ame[i]}
Looping associ­ative arrays
for (var keyEtu­diant in tabEtu­diant) {print keyEtu­dia­nt+­"­:"+t­abE­tud­ian­t[k­eyE­tud­iant]}
Array methods
arrayN­ame.to­Str­ing()
convert array contents to string
arrayN­ame.jo­in(­" seperator ")
same as above: elem1 elem2 elem3
arrayN­ame.pop()
removes last elem
arrayN­ame.pu­h(elem)
add new element to end of array
arrayN­ame.sh­ift()
removes first elem from array | returns value of arrayN­ame[0]
arrayN­ame.un­shi­ft(­elem)
adds new elem to begining of array
arrayN­ame.length
returns number of elems in array
elete arrayN­ame[0]
changes first elem of array to undefined
arrayN­ame.sp­lic­e(p­osi­tion, numElems, elemTo­Add1, elemen­tTo­Add2)
add nzw items to an array
arrayN­ame.sp­lic­e(s­tar­tin­gPo­s,n­umE­lem­esT­oDe­lete)
removes number­OfElems to delete starting from starti­ngPos
var pets = catsAr­ray.co­nca­t(d­ogA­rray);
joins two array together into one
elem.i­nne­rHTML = arrayName;
replaces HTML content of elem by string containing the elems of arrayName
Sorting arrays
arrayN­ame.sort()
sorts elems from less important to most important
arrayN­ame.re­verse()
sorts most important to less important