JQUERY CHEAT SHEET - intro
#
Window Onload$(() => { // put jQuery in here});
#
Search / Retrievalselect all elements of type:
$('div')
,$('p')
, etc.you can get the body of the document with:
$('body')
get element by id:
$('#idName')
get elements by class:
$('.className')
get a specific jQuery element from a list:
jQueryCollection.eq( indexNum )
#
Creationcreate element:
$('<div>')
,$('<p>')
, etc.create element with more information:
$('<p> this is a p with text inside it </p>')
;
#
Appending and removingappend elements:
jQueryElement.append( jQueryElement )
prepend elements:
jQueryElement.prepend( jQueryElement )
remove elements:
jQueryElement.remove()
empty an element of content:
jQueryElement.empty()
append an element with the order reversed:
jQueryElement.appendTo( jQueryElement )
clone an element:
jQueryElement.clone()
#
Traversalget the parent of an element:
jQueryElement.parent()
get child elements:
jQueryElement.children()
#
Setting and checkingset any attribute on an element (example -- src):
someImg.attr('src', 'someURL')
set id on element:
jQueryElement.attr('id', 'idName')
set class on element:
jQueryElement.addClass('className')
set text inside element:
jQueryElement.text("some text")
set html inside element:
jQueryElement.html("<some html>")
check if element has a class:
jQueryElement.hasClass('someClass')
set a css property on an element:
jQueryElement.css('property', 'value')
remove a class from an element:
jQueryElement.removeClass('someClass');
#
Eventsset an event listener:
jQueryElement.on('event_type', EVENT_HANDLER);
example:
jQueryElement.on('click', () => {});
set a click event specifically:
jQueryElement.click(EVENT_HANDLER);