jQuery Flashcards
What is jQuery?
jQuery is a library. A library is code that someone else wrote that you can incorporate into your own projects to improve them or make coding easier.
What is the shorthand way to write jQuery?
The dollar sign $
Where should the script tag for the jQuery library be placed?
In the body of our body tag right above our javascript script tag.
What’s a way of ensuring that our javascript library does not access our jQuery library until it is ready (when script tag is placed in the head tag)?
We can use the following code:
$(document).ready(function () {
$(“h1”).css(“color”, “red”);
})
What’s a second way of ensuring that our javascript library does not access our jQuery library until it is ready?
By including the script tag at the bottom of our webpage (bottom of body section above our javascript script tag).
How does minification work to reduce file size?
Minification removes whitespace and comments in your code. The link in our jQuery script tag takes us to to a minimized jQuery library.
Why do we minifiy our code?
All of our code has to be loaded by our user’s browser and depending on their internet speed that may take a short or long time. Minifying helps our code to be loaded much faster.
The browser does not care about comments or spaces.
How do we select elements with jQuery?
We replace the document.querySelector with the keyword jQuery(or the dollar sign).
Ex:
javascript: document.querySelector(“h1.title”)
jQuery: $(“h1.title”)
How do we select multiple elements in jQuery?
By using the same syntax for selecting a single element.
Ex:
javascript: document.querySelectorAll(“button”)
jQuery: $(“button”)
How can we manipulate (set) the style of an element with jQuery?
By using the .css method.
Ex:
$(“h1”).css(“color”, “red”);
The 1st input is the property we want to change and the 2nd input is the value we want to set for the property.
How can we get the current value for a css property?
By having the name of the property as a string within the css jQuery method and logging it to the console.
Example:
console.log($(“h1”).css(“font-size”));
How can we keep our css (styling) and our javascript (behavior) separate?
By first creating a class in our stylesheet and then adding that class with javascript.
Example:
In css:
.big-title {
font-size: 10rem;
color: yellow;
font-family: cursive;
}
In javascript:
$(“h1”).addClass(“big-title”);
How do we add multiple classes in jQuery?
We add multiple classes in jQuery by adding a space in between the classes.
Example:
$(“h1”).addClass(“big-title margin-50”);
Adds both the big-title class and margin-50 class.
How can we see if a selected element has a certain class in jQuery?
By selecting our element and using the hasClass method and query for the specific class.
Example:
$(“h1”).hasClass(“big-title”);
How can we change the text of a selected element using jQuery? (two methods)
1st method:
Select the element than place the new text wanted inside of the text method.
Example:
$(“h1”).text(“bye”);
2nd method
Changing the inner html by using the html method.
Example:
$(“h1”).html(“<em>bye</em>”);
Note: Javascript uses the innerHTML method.