jQuery Cheat Sheet

In the world of web development, JavaScript is one of the most popular programming languages. It is used to create interactive and dynamic web pages that can respond to user actions. However, writing JavaScript code from scratch can be time-consuming and challenging, especially for beginners. This is where jQuery comes in.

jQuery is a fast, lightweight, and feature-rich JavaScript library that simplifies the process of creating dynamic web pages. It was created by John Resig in 2006 and has since become one of the most widely used JavaScript libraries in the world.

What is jQuery?

jQuery is a free, open-source JavaScript library that simplifies HTML document traversal and manipulation, event handling, and animation. It provides a set of easy-to-use functions that allow developers to write less code and achieve more in less time.

jQuery is designed to work seamlessly with HTML and CSS, making it easy to add interactivity and animation to web pages. It also provides cross-browser compatibility, which means that your code will work on all major web browsers, including Chrome, Firefox, Safari, and Internet Explorer.

Why use jQuery?

There are several reasons why jQuery is so popular among web developers:

1. Simplifies JavaScript code: jQuery provides a set of easy-to-use functions that simplify the process of writing JavaScript code. This means that developers can achieve more in less time, without having to write complex code from scratch.

2. Cross-browser compatibility: jQuery is designed to work seamlessly with all major web browsers, ensuring that your code will work on any device.

3. Animation and interactivity: jQuery provides a range of animation and interactivity features that can be easily added to web pages. This includes effects such as fading, sliding, and toggling.

4. Large community: jQuery has a large and active community of developers who contribute to the library and provide support to other developers.

Getting started with jQuery

To get started with jQuery, you need to include the jQuery library in your HTML document. You can do this by downloading the library from the jQuery website or by using a CDN (Content Delivery Network).

Once you have included the library, you can start using jQuery functions to manipulate HTML elements, handle events, and add animation and interactivity to your web pages.

Here is an example of how to use jQuery to hide and show a div element:


<!DOCTYPE html>
<html>
<head>
<title>jQuery Example</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$("#hide").click(function(){
$("#content").hide();
});
$("#show").click(function(){
$("#content").show();
});
});
</script>
</head>
<body>
<button id="hide">Hide Content</button>
<button id="show">Show Content</button>
<div id="content">
<p>This is some content that can be hidden or shown using jQuery.</p>
</div>
</body>
</html>

In this example, we have included the jQuery library using a CDN. We have then used jQuery functions to handle the click events of two buttons. When the “Hide Content” button is clicked, the content div is hidden using the `hide()` function. When the “Show Content” button is clicked, the content div is shown using the `show()` function.

jQuery is a powerful JavaScript library that simplifies the process of creating dynamic and interactive web pages. It provides a range of easy-to-use functions that allow developers to achieve more in less time. With its cross-browser compatibility and large community, jQuery is a must-have tool for any web developer.

Cheat Sheet

TopicDescription
SelectorsUsed to select and manipulate HTML elements
EventsUsed to trigger actions when certain events occur
EffectsUsed to create animations and transitions
DOM ManipulationUsed to add, remove, and modify HTML elements
AJAXUsed to send and receive data asynchronously
UtilitiesMiscellaneous functions for common tasks
SelectorDescription
`$(element)`Selects all elements with the specified tag name
`$(#id)`Selects the element with the specified ID
`$(.class)`Selects all elements with the specified class
`$(selector1, selector2)`Selects elements that match either selector1 or selector2
`$(parent > child)`Selects all direct child elements of the parent element
`$(prev + next)`Selects the next element after the prev element
`$(prev ~ siblings)`Selects all sibling elements after the prev element
EventDescription
`click()`Triggers when an element is clicked
`dblclick()`Triggers when an element is double-clicked
`mouseenter()`Triggers when the mouse enters an element
`mouseleave()`Triggers when the mouse leaves an element
`keydown()`Triggers when a key is pressed down
`keyup()`Triggers when a key is released
`submit()`Triggers when a form is submitted
EffectDescription
`hide()`Hides the selected element
`show()`Shows the selected element
`toggle()`Toggles between hiding and showing the selected element
`fadeIn()`Fades in the selected element
`fadeOut()`Fades out the selected element
`slideUp()`Slides up the selected element
`slideDown()`Slides down the selected element

DOM Manipulation

FunctionDescription
`append()`Adds content to the end of the selected element
`prepend()`Adds content to the beginning of the selected element
`after()`Adds content after the selected element
`before()`Adds content before the selected element
`remove()`Removes the selected element
`empty()`Removes all child elements and content from the selected element
`attr()`Gets or sets the value of an attribute for the selected element

AJAX

FunctionDescription
`$.ajax()`Performs an AJAX request
`$.get()`Performs an AJAX GET request
`$.post()`Performs an AJAX POST request
`$.getJSON()`Performs an AJAX request and returns JSON data

Utilities

FunctionDescription
`$.each()`Iterates over an array or object
`$.grep()`Filters an array based on a condition
`$.map()`Transforms an array
`$.extend()`Merges two or more objects
`$.trim()`Removes whitespace from the beginning and end of a string

Reference:

https://api.jquery.com/