title: jQuery date: 2020-12-24 21:08:21 background: bg-[#2c63a2] tags: - web - js - javascript - library categories: - Programming intro: | This jQuery cheat sheet is a great reference for both beginners and experienced developers. plugins:

- tooltip

Getting Started

Including jQuery

```javascript {.wrap}

#### Official CDN
```javascript {.wrap}
<script src="https://code.jquery.com/jquery-3.5.1.min.js" crossorigin="anonymous"></script>

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 {.secondary}

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

Combining selectors

$("selector1, selector2 ...selectorn")

Basics

Basic Filters

Attribute

Child Filters

Forms

jQuery Attributes

Examples {.secondary .row-span-2}

$('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 {.secondary .row-span-3}

/*<span>Span.</span>*/
$('span').after('<p>Paragraph.</p>');
/*<span>Span.</span><p>Paragraph.</p>*/

/*<span>Span.</span>*/
$('<span>Span.</span>').replaceAll('p');
/*<p>Span.</p>*/

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

Copying

DOM Insertion, Around

DOM Insertion, Inside

DOM Insertion, Outside

DOM Removal

DOM Replacement

jQuery Traversing

Filtering

Miscellaneous Traversing

Tree Traversal

jQuery Events

Examples {.secondary .row-span-6}

// 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 {.row-span-6}

Document Loading

Event Handler Attachment

Form Events

Keyboard Events

Mouse Events

jQuery Effects

Examples {.secondary .row-span-2}

$('#menu-button').on('click', () => {
  // $('#menu').fadeIn(400, 'swing')
  $('#menu').fadeIn();
});

fadeOut effect

$('#menu-button').on('click', () => {
  // $('#menu').fadeOut(400, 'swing')
  $('#menu').fadeOut();
});

Basics

Sliding

Custom

Fading

jQuery Ajax

Examples {.secondary .row-span-2}

$.ajax({
  url: this.action,
  type: this.method,
  data: $(this).serialize()
}).done(function(server_data){
  console.log("success" + server_data);
}).fail(function(jqXHR, status, err){
  console.log("fail" + err);
});

Global Ajax Event Handlers

Helper Functions

Low-Level Interface

Shorthand Methods

Miscellaneous

jQuery Object

Deferred Object {.row-span-2}

Utilities {.row-span-3}

DOM Element Methods

Internals

Callbacks Object