jQuery Flashcards
What is jQuery?
The most popular JS library Takes the traditional DOM manipulation methods, and makes it easier and efficient in fewer lines of code Example: Traditional document.querySelector(“h1”) JQuery $(“h1”)
How to incorporate jQuery into Websites
Easiest way is to use a CDN method
CDN is basically a network of connected servers. CDN then finds the easiest and fastest method to retrieve files.
Place the jQuery CDN script above the javascript at directly above the ending body tag
Selecting Elements with jQuery
jQuery(“ < CSS Selector > ”) OR $(“ < CSS Selector > ”) $() == jQuery() There is no difference between selecting all elements vs one element
Manipulating Styles with…
jQuery
To set the style
.css(< Attribute to change>, )
Example
$(“h1”).css(“color”, “red”);
To get the style
.css(< Attribute to get >);
Separate concerns
jQuery is implemented in the .js file, and we can also use it to style the page
But styling the page in a .js file is bad practice
So the correct thing to do is create a css class inside the styles.css, and use jQuery to set the style to that class in the styles.css via .addClass()
Add/Remove class
.addClass(< CSS class to change style to, can be multiple classes which are separated by a single space>); .removeClass(< CSS class to change style to, can be multiple classes which are separated by a single space >);
Check if an element has a class
.hasClass( < class to check if exists > )
Manipulating Text with jQuery
2 Methods
- .text( < New Text> );
- .html(< New HTML >);
Manipulating Attributes with…
jQuery
Set Attribute
.attr(< Attribute to change>, < New Attribute Value>)
Get Attribute
.attr(< Attribute to check>)
Adding Event Listeners with…
…jQuery
Click Listener
.click( < Callback function> );