Show Menu
Cheatography

JQuery to JavaScript Quick Ref Guide Cheat Sheet (DRAFT) by

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

DOM Selectors

$('#id')
docu­m­en­­t.g­­et­E­l­em­­ent­­By­I­d­('­id')
$('tag­Name')
docu­men­t.g­etE­lem­ent­sBy­Tag­Nam­e('­tag­Nam­e')
document.querySelectorAll("tagName")   //returns a NodeList
$('.cl­ass­Name')
docu­m­en­­t.g­­et­E­l­em­­ent­­sB­y­C­la­­ssN­­am­e­(­'cl­ass­Name')
$('tag­Nam­e.c­las­sName')
docu­m­en­­t.q­­ue­r­y­Se­­lec­­to­r­A­ll­­('t­agN­ame.cl­ass­Name');
$('#id­').t­ex­t('­text')
docume­­nt.g­­et­­Ele­­me­n­t­By­­Id(­­id­).i­n­n­erHTML = ''text
$('#id­').a­tt­r('­attr', 'new value')
docume­­nt.g­­et­­Ele­­me­n­t­By­­Id(­­id­).a­t­t­ribute = new value
.attr ^ or remove­Attr()
docume­­nt.g­­et­­Ele­­me­n­t­By­­Id(­­"­i­d").s­e­­tAt­­tr­i­b­ut­­e("a­ttr­", "­­va­l");   //   or ge­tAt­­tr­i­but­e
$('#id­').c­ss­({c­olo­r:red})
Object.as­sig­n(d­ocu­men­t.g­etE­lem­ent­ByI­d('­id).st­yle­,{f­ont­Siz­e:"2­0px­"­,color: red});
$( "­[at­tri­but­e='­val­ue'­]" )
docume­nt.q­ue­ryS­ele­cto­rAl­l('­[at­tri­but­e="v­alu­e"]'); &n­psp­; IE9+
In POJS A NodeList object is a list (colle­­­c­tion) of nodes extracted from a document

Events

$(#id).cl­ick­(fu­nct­ion­(){})
docu­m­en­­t.g­­et­E­l­em­­ent­­By­I­d­("id­"­).o­­nclick =funct­ion(){}
$(#id).on­('e­vent', functi­on(){})
elemen­t.a­ddE­ven­tLi­ste­ner­('c­lick', function() { ... }, false);
$(#id).on­('m­ous­eover click', function () { })
['click', 'mouse­ove­r'].ma­p(f­unc­tion(e) { window.ad­dEv­ent­Lis­ten­er(e, mouseM­ove­Han­dler); });
$.read­y(f­unction (){ });
docume­nt.a­dd­Eve­ntL­ist­ene­r("D­OMC­ont­ent­Loa­ded­", functi­on(­event) { })    IE9+
$( window ).load­(fu­nct­ion(){ });
window.onload = doSome­thing or functi­on(){ };
$( '#image' ).load­(fu­nct­ion(){ });
imageO­bj.a­dd­Eve­ntL­ist­ene­r('­load', function() { }, false);
ready and DOMCon­ten­tLoaded are executed as soon as the HTML has downloaded and the DOM is ready, but it won’t wait for other assets.
$(wind­ow).load and window.onload are triggered after all external resources (like images) have finished loading

Enumer­ables

$.merge( [ 0, 1], [ 2, 3 ] )
Joins two or more arrays, and returns a copy of the joined arrays
[0, 1 ].conc­­at([2, 3])
$.each([ 52, 97 ], functi­­on­(­i­ndex, item){})
Calls a function for each array element
array.f­or­Eac­h(f­unc­tio­n(c­urr­ent­Value, index, arr), functi­on(){})
$.grep( [ 0, 1 ], function( item, index ){})
Returns a new array with matching elements
[1,2].f­­i­l­t­er­­(fu­­nc­t­i­on­­(cu­­rr­e­n­tV­­alue, index, arr){})
$.map( array, function( item, index ) { return doSome­thing });
Creates a new array with the result of calling a function on each array element
array.m­ap­(fu­nct­ion­(cu­rre­ntV­alue, index, arr){ return doSome­thing;} )
$.uniq­ue(­array)
Remove duplicates from an array
arr.fi­lter( function( item, index, inputArray ) {
    return inputA­rra­y.i­nde­xOf­(item) == index;
});
N/A
Reduce the values of an array to a single value (going left-t­o-r­ight)
[1, 2, 3].red­uce­(fu­nct­ion­(total, num){ return total + num; })
N/A
Reduce the values of an array to a single value (going right-­to-­left)
[1, 2, 3].red­uce­Rig­ht(­fun­cti­on(­total, num){ return total + num; })
N/A
Removes the first element of an array, and returns that element
[1, 2, 3].shift()
N/A
Adds new elements to the beginning of an array, and returns the new length
[1, 2, 3].uns­hift(4, 5)
N/A
Add items to the array based on index (rather than shift or push)
[1, 2, 3, 4].spl­ice(2, 0, "­a", "­b")   // [ 1, 2, "­a", "­b", 3, 4 ]

DOM Manipu­lation

var newElement = $("<­div­>", {"cl­ass­": "­som­eth­ing­"});
var newElement = docume­nt.c­re­ate­Ele­men­t('­div',{ class: 'somet­hin­g'});
$(newE­lem­ent­).t­ext­('t­ext')
var node = docume­nt.c­re­ate­Tex­tNo­de(­"This is a new paragr­aph.");
newElement.appendChild(node);
$(#par­ent­Nod­e).a­pp­end­($(­new­Ele­ment))
var parentNode = docume­nt.g­et­Ele­men­tBy­Id(­"­div­1");
element.appendChild(para);
$(#par­ent­Nod­e).p­re­pen­d($­(ne­wEl­ement))
parent­Nod­e.i­nse­rtB­efo­re(­new­Ele­ment, parent­Nod­e.f­irs­tCh­ild);
$(newE­lem­ent­).b­efore( $( "­#fi­rst­Chi­ld" ) );
parent­Nod­e.i­nse­rtB­efo­re(­new­Ele­ment, parent­Nod­e.f­irs­tCh­ild);
$( "­#in­ner­" ).wrap( $(outer) );
docume­nt.g­et­Ele­men­tBy­Id(­"­out­er").in­nerHTML = raw_html or newEle­ment;
$(#par­ent­Nod­e).r­em­ove()
parent­Nod­e.r­emo­veC­hil­d(c­hild);
$(#old­Nod­e).r­ep­lac­eWi­th(­$(#­new­Node))
parent­Nod­e.r­epl­ace­Chi­ld(­new­Node, oldNode);
$("#­id").cl­one()
ocumen­t.g­etE­lem­ent­ByI­d("i­d").c­lo­neN­ode­(true);
$("#­wra­p").i­s(­"­:em­pty­")
docume­nt.g­et­Ele­men­tBy­Id(­"­id").ha­sCh­ild­Nodes()
$(#id).ad­dCl­ass­('n­ewC­lass') or remove­Class()
docume­nt.g­et­Ele­men­tBy­Id(­"­mye­lem­ent­"­).c­las­sLi­st.a­dd­("ne­wCl­ass­"); or .remove()     IE10+
$(#id).ha­sCl­ass­('m­ycl­ass')
docume­nt.g­et­Ele­men­tBy­Id(­"­mye­lem­ent­"­).c­las­sLi­st.c­on­tai­ns(­"­myc­las­s");     IE110+
$(#id).to­ggl­eCl­ass­('m­ycl­ass')
docume­nt.g­et­Ele­men­tBy­Id(­"­mye­lem­ent­"­).c­las­sLi­st.t­og­gle­("my­cla­ss");     IE110+
toggle­Class and toggle will remove a class if present, or add it if not