JQuery Cheatsheet By Chloe
Write less, do more.
$(document).ready(function(){ ALL CUSTOM JQUERY GOES HERE! });Jquery is called and represented by the '$'.
$("selector").method;Please note that double quotations are preferred when using Jquery.
Selectors:
Similarly to JS, we tell Jquery which elements we want to work on by using selectors, below are a few of the more common ones:SYNTAX | WHAT IT SELECTS |
---|---|
$("*"); | Every element on the page. |
$("this"); | Current element being operated on in a function. |
$("p"); | Selects all the tags of specified element |
$(".class"); | Class selector |
$("#id"); | ID selector |
$("[type = 'text']"); | Attribute selector. Selects any element with text applied to type attr. |
$("p: first-of-type"); | Selects first P tag. |
JQuery Events
Events are what make the sites interactive. JQuery events are similar to the JS 'addEventListener' method but require less code.$(document).ready(function(){ $("#clkEx").click(function(){ $("#clkEx").css("backgroundColor", "orange"); }); });The JS code for it was:
let cEx = document.getElementById('clkEx'); let changeBack = () => { cEx.style.backgroundColor = 'orange'; } cEx.addEventListener('click', changeBack);As you can see Jquery requires only two lines of code (if you remove the document.ready line) and does not require any new variables to be made, as opposed to the JS code which requires a new variable to be declared and a new function to be made.
SYNTAX | WHAT IT DOES |
---|---|
click() | Executes when element is clicked once. |
hover() | Executes when mouse is hovered over element. |
submit() | Executes when form is submitted. |
scroll() | Executes when screen is scrolled by user |
keydown() | Executes when a key is pressed on the keyboard |
JQuery Effects
Effects are used hand-in-hand with events to allow animations on the page. We use the Jquery events to listen for the users actions, we then use the Jquery effects to add more animation to the page.SYNTAX | WHAT IT DOES |
---|---|
toggle() | Switches the visibility of an element between show and hide. |
fadeToggle() | Switches the visibility of an element and animates the opacity. |
slideToggle() | Switches the visibility of an element using a sliding effect. |
animate() | Performs custom animation effects on the CSS of an element. |
.modal { display: hide; height: 200px; width: 200px; background-color: black; font-size: 20px; font-family: 'Roboto Mono', monospace; color: white; margin: auto; } $("#modalTrigger").click(function(){ $(".modal").toggle(); });