jQuery cheatsheet

This jQuery cheat sheet is a great reference for both beginners and experienced developers.

#Getting Started

#Including jQuery

 

#Official CDN

 

#jQuery syntax

 $(selector).methodOrFunction(); 

#Example:

$('#menu').on('click', () =>{ $(this).hide(); }); $("body").css("background", "red"); 

#jQuery document ready

 $(document).ready(function( ) { // Runs after the DOM is loaded. alert('DOM fully loaded!'); }); 
$(function( ){ // Runs after the DOM is loaded. alert('DOM fully loaded!'); }); 

#jQuery Selectors

#Examples

$("button").click(() => { $(":button").css("color", "red"); }); 

#Combining selectors

$("selector1, selector2 . selectorn") 

#Basics

#Basic Filters

#Attribute

#Child Filters

#Forms

#jQuery Attributes

#Examples

$('h2').css({ color: 'blue', backgroundColor: 'gray', fontSize: '24px' }); 

#jQuery addClass

$('.button').addClass('active'); 

#jQuery removeClass

$('.button').on('mouseleave', evt => { let e = evt.currentTarget; $(e).removeClass('active'); }); 

#jQuery .toggleClass

$('.choice').toggleClass('highlighted'); 

#Attributes

#Data

#CSS

#Dimensions

#Offset

#jQuery Manipulation

#Examples

/*Span.*/ $('span').after('

Paragraph.

'
); /*Span.

Paragraph.

*/
/*Span.*/ $('Span.').replaceAll('p'); /*

Span.

*/
/*This is span.*/ $('span').wrap(' '); /*

This is span.

*/

#Copying

#DOM Insertion, Around

#DOM Insertion, Inside

#DOM Insertion, Outside

#DOM Removal

#DOM Replacement

#jQuery Traversing

#Filtering

#Miscellaneous Traversing

#Tree Traversal

#jQuery Events

#Examples

// A mouse event 'click' $('#menu-button').on('click', () => { $('#menu').show(); }); // A keyboard event 'keyup' $('#textbox').on('keyup', () => { $('#menu').show(); }); // A scroll event 'scroll' $('#menu-button').on('scroll', () => { $('#menu').show(); }); 

#Event object

$('#menu').on('click', event => { $(event.currentTarget).hide(); }); 

#Method chaining

$('#menu-btn').on('mouseenter', () => { $('#menu').show(); }).on('mouseleave', () => { $('#menu').hide(); }); 

#Prevents the event

$( "p" ).click(function( event ) { event.stopPropagation(); // Do something }); 

#Browser Events

#Event Object