Chapter 7: jQuery Flashcards
What is JQuery?
It’s a javascript file that you include in your web pages. It let’s you find elements using cs style selectors and then do something with the selection using methods.
What happens when you make a selection
It’s stored in a jQuery object.
How do I make a jQuery seelction
$(‘css-slector’)
e.g. $(‘li.hot’);
How do you use a jQuery method
Like an object methods, by do notation
e.g. $(‘li.hot’).addClass(‘complete’);
Why use jQuery?
- Simple selectors
- common tasks in less code
- cross browser compatibility
What are other ways of describing a jQuery object?
matched set
jQuery selection
How can you access a specific item in a jQuery selection
Array notation
Some jQuery methods get and set element contents. What needs to be kept in mind?
If a jquery object has multiple elements, and we a method, say .html(). only the content from the first element is returned
If we use the html() method to update contents of a jQuery selection it changes all elements in the jQuery selection.
What can we do to make variables containing jQuery selections more easily recognised?
giver it a $sign at start of name
What is implicit iteration?
jQuery’s ability to automatically update all matching nodes in a jQuery object (No need to loop through the results)
What happens if we have an object that has a long list of chained methods and one of those methods is incorrectly spelt?
None will run.
If one does not run, none will.
How do we check if DOM is ready to work with?
load event = once DOM and all resources and media loaded
.ready() = as soon as DOM loaded
$( document).ready(function(){ //Your script });
or the shorthand $(function(){ //Your script });
What do the .html() and .text() methods do?
Retrieve and update content of elements
Caution with .html() - XSS
.html() - retrieves only the html inside the first match
.text() - retirves text from all matching elements plus their decedents
Why do we need to be careful using .replaceWith and .html() methods?
Same risks as innerHTML - XSS
What are four methods to update content of a jQuery Object?
.html() - updates all elements html
.text() - updates all element text
.replaceWith() - adds new content and returns replaced content
.remove() - removes the matching elements