jQuery Flashcards
5 things that jquery makes it easy to do
Find - Elements in an HTML document
Change - HTML content
Listen - to what a user does and react accordingly
Animate content on a page
Talk - over the network to fetch new content
find an h1 in the DOM using jQuery and return the text that it contains
jQuery("h1"); (important to not that it is a string wrapped in "") or $("h1").text(); $("#idName").text(); $(".className").text();
find an h1 and change/add the text
$(“h1”).text(“new text here”);
note: need to make sure the DOM has finished loading the HTML content before we can reliably use jQuery
how to make sure that jquery is only run when the DOM is finished loading?
use the “ready” method. example:
jQuery(document).ready(functyion(){
$(“h1”).text(“new text here”);
});
how to get all of the li in an HTML list
$(“li”);
It returns an object with all of the line items
how to get the li of a of the “destinations” class
$(“.destinations li”);
if there are li’s that are children of the destination’s li’s then we will need to use the decedent selector if you want only the lis that are the direct children of destinations class. like this:
$(“.destinations > li”);
selecting multiple elements or specific elements in the array
multiple elements
$(.className, #idName);
specific list items
$(li: first): would select the first li
can also use: last, odd, even
how to find elements by traversing the DOM
$("#destinations").find("li"); or $("li").first(); or $("li").last();
how to walk the DOM (get to the next or previous li for example)?
$(“li”).first().next();
$(“li”).last().prev();
how to “walk up” or “walk down” the DOM
$(“li”).parent();
$(“li”).children();
how to add (4 methods) elements of the DOM
Adding
var. appendTo(“element”);
var. prependTo(“element”);
var. insertAfter(“element”);
var. insertBefore(“element”);
Or $('item already in DOM').append('element') $('item').prepend('element') $('item').after('element') $('item').before('element')
How to remove an item from the DOM
$(‘item’).remove()
How to add an event listener to execute a function when a button is clicked.
Additionally, how would you only change the button that was clocked (as opposed to all of the buttons?
$('button').on('click', function(){ //function here })
To apply it to only the button that was clicked…
How to add an event listener to execute a function when a button is clicked.
Additionally, how would you only change the button that was clocked (as opposed to all of the buttons?
$('button').on('click', function(){ //function here })
To apply it to only the button that was clicked…
How to add an event listener to execute a function when a button is clicked.
Additionally, how would you only change the button that was clocked (as opposed to all of the buttons?
$('button').on('click', function(){ //function here })
To apply it to only the button that was clicked…