Certainly! Below is a basic jQuery cheat sheet that includes some commonly used functions and syntax:
Getting Started:
- Include jQuery:
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
- Document Ready:
$(document).ready(function() {
// Your code here
});
// Shorthand for document ready
$(function() {
// Your code here
});
Selectors:
- Element Selector:
$("element")
- ID Selector:
$("#id")
- Class Selector:
$(".class")
- Attribute Selector:
$("[attribute='value']")
DOM Manipulation:
- Get/Set HTML content:
// Get
var content = $("selector").html();
// Set
$("selector").html("new content");
- Get/Set Text content:
// Get
var text = $("selector").text();
// Set
$("selector").text("new text");
- Get/Set Attribute:
// Get
var value = $("selector").attr("attribute");
// Set
$("selector").attr("attribute", "new value");
- Add/Remove Class:
$("selector").addClass("new-class");
$("selector").removeClass("old-class");
- Toggle Class:
$("selector").toggleClass("class-to-toggle");
- Append/Prepend content:
$("selector").append("new content");
$("selector").prepend("new content");
- Remove element:
$("selector").remove();
Events:
- Click Event:
$("selector").click(function() {
// Your code here
});
- Mouse Enter/Leave:
$("selector").mouseenter(function() {
// Your code here
});
$("selector").mouseleave(function() {
// Your code here
});
- Form Submission:
$("form-selector").submit(function(event) {
// Your code here
event.preventDefault(); // Prevents the default form submission
});
- Key Press:
$("input-selector").keypress(function(event) {
// Your code here
});
Effects and Animations:
- Hide/Show:
$("selector").hide();
$("selector").show();
- Toggle Visibility:
$("selector").toggle();
- Fade In/Out:
$("selector").fadeIn();
$("selector").fadeOut();
- Slide Up/Down:
$("selector").slideUp();
$("selector").slideDown();
- Animate:
$("selector").animate({ property: value }, duration);
This cheat sheet covers some of the basics, but jQuery has many more features and functions. For more detailed information, refer to the official jQuery documentation.
Show Comments