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> );
Key Press Listener
.keypress( < Callback function> );
General/Flexible Method
.on( < Event > ,< Callback function> );
Adding and Removing Elements with…
jQuery
Adding an Element
Adding a new element before a selected element
.before(< HTML of New Element >);
Adding a new element after a selected element
.after(< HTML of New Element >);
Prepend a new element inside of a selected element
.prepend(< HTML of New Element >);
Append a new element inside of a selected element
.append(< HTML of New Element >);
Web Animations with….
jQuery
Hiding an element
.hide();
Element gets removed from the flow of the HTML structure of the web page
Show an element
.show();
Toggle hide and show
.toggle();
FadeOut
Hides the element but in a less sudden way
.fadeOut();
FadeIn
Shows the element but in a less sudden way
.fadeIn();
Toggle fade in and fade out
.fadeToggle();
Slide Up
Collapses an element
.slideUp();
Slide Down
Uncollapse an element
.slideDown();
Toggle Slide In and Slide Out
.slideToggle();
Method for more Control Over Animations
.animate( { < New CSS Rule to Animate>});
Can only add CSS rules that have a numeric value
Can also chain animation methods back to back
Example:
$(“h1”).slideUp().slideDown().animate({opacity: 0.5});