WILL EDIT AND FORMAT REMAINING EXERCISES LATER -Sally + Jeff
$(“TAGNAME”).text()
$(“TAGNAME”).text(“YOUR NEW TEXT HERE”)
> $(document).ready(function() {
});
$(“#blah”)
$(“.blah”)
$(“ANCESTOR DESCENDANT”)
$(“parent>child”)
$(“SELECTOR1, SELECTOR2, …, SELECTORN”)
EXAMPLE: :first
$(“TAG/CLASS:first”)
> $(“#IDNAME TAGNAME:first”)
Within #tours list, find the direct children li elements, and then use the :even pseudo selector to select every other li element.
$("#tours>li:even")
select all vacations from America using $("#vacations .america"). This works, but let's change the previous code to use traversal instead with the find method.
$("#vacations").find(".america")
$("#vacations .america")
####TRAVERSAL:
$("#vacations").find(".america")
Using traversal or filtering, select the first vacation li element from the list. (No using pseudo selectors!)
Using traversal:
$("#vacations").find("li").first()
someone else is selecting the last vacation with $("#vacations li:last"). refactor this code to find the last li within #vacations using traversal
Using traversal: $("#vacations").find("li").last()
vacation that is previous to the last one will always be on sale. Use traversal with the prev() method to select the vacation that is right before the last one.
$(“#vacations”).find(“li”).last().prev()
Using traversal, select all tours that have a .featured class on their title by getting the parent() of featured titles.
$(".featured").parent()
you used to select all the tours: $("#tours > li"). You immediately realize it can be done better, refactor this to use traversal with children().
$("#tours").children()
//updating the dom: adding an element to the document with attributes and text //GOAL: show message to traveler letting them know our phone # to book the trip. Let's add the phone number immediately before() the "Book Now" button. You can check out the HTML of the rendered page by clicking on the HTML tab below. //HTML:
<div id="tours">
<h1>Guided Tours</h1>
<ul>
<li class="usa tour">
<h2>New York, New York</h2>
<span class="details">$1,899 for 7 nights</span>
<button class="book">Book Now</button>
</li>
</ul>
</div>
//STEP 1: create span
node with phone #
var message = $('Call 1-555-jquery-air to book this tour');
//STEP 2: select the item you want to append/prepend the new element to
var book_now_button = $(".book:first");
//STEP 3: do it
book_now_button.before(message);
(or book_now_button.append(message); for appending)
//adding to the dom: //GOAL: append() our to the bottom of the .usa element. Let's change the code to add it there instead. //HTML:
<div id="tours">
<h1>Guided Tours</h1>
<ul>
<li class="usa tour">
<h2>New York, New York</h2>
<span class="details">$1,899 for 7 nights</span>
<button class="book">Book Now</button>
</li>
<li class="europe tour">
<h2>Paris, France</h2>
<span class="details">$2,299 for 7 nights</span>
<button class="book">Book Now</button>
</li>
<li class="asia tour">
<h2>Tokyo, Japan</h2>
<span class="details">$3,799 for 7 nights</span>
<button class="book">Book Now</button>
</li>
</ul>
</div>
//SOLUTION: var message = $('Call 1-555-jquery-air to book this tour'); var usa_elt = $('.usa:first'); usa_elt.append(message);
//removing from the dom: //GOAL: remove that "Book Now" button until we can implement it. Go ahead and remove() it from the page. //HTML:
<div id="tours">
<h1>Guided Tours</h1>
<ul>
<li class="usa tour">
<h2>New York, New York</h2>
<span class="details">$1,899 for 7 nights</span>
<button class="book">Book Now</button>
<span>Call 1-555-jquery-air to book this tour</span></li>
</ul>
</div>
//SOLUTION: (select and then remove)
$('.book').remove();
//interaction using dom:
//GOAL: wrap all previous code in click handler for any button
elements using on()
method
//HTML:
<div id="tours">
<h1>Guided Tours</h1>
<ul>
<li class="usa tour">
<h2>New York, New York</h2>
<span class="details">$1,899 for 7 nights</span>
<span>Call 1-555-jquery-air to book this tour</span></li>
</ul>
</div>
//SOLUTION: $('button').on('click', function(){
var message = $('Call 1-555-jquery-air to book this tour');
});
//GOAL: Acting on Click: Change click handler to trigger when the .tour list item is clicked instead //HTML:
<div id="tours">
<h1>Guided Tours</h1>
<ul>
<li class="usa tour">
<h2>New York, New York</h2>
<span class="details">$1,899 for 7 nights</span>
<button class="book">Book Now</button>
</li>
</ul>
</div>
//SOLUTION:
//GOAL: On Page Load: Wrap all code in
//GOAL: Removing the Clicked Button: Just remove the one button that was clicked by using this
//SOLUTION:
//GOAL: Relative Traversing I
//SOLUTION:
//GOAL: Relative Traversing II someone changed our HTML. Now the is inside a
<div id="tours">
<h1>Guided Tours</h1>
<ul>
<li class="usa tour">
<h2>New York, New York</h2>
<span class="details">$1,899 for 7 nights</span>
<div>
<button class="book">Book Now</button>
</div>
</li>
<li class="europe tour">
<h2>Paris, France</h2>
<span class="details">$2,299 for 7 nights</span>
<div>
<button class="book">Book Now</button>
</div>
</li>
<li class="asia tour">
<h2>Tokyo, Japan</h2>
<span class="details">$3,799 for 7 nights</span>
<div>
<button class="book">Book Now</button>
</div>
</li>
</ul>
</div>
//SOLUTION:
//GOAL: Relative Traversing III Rather than clicking on the button to show the message, we've decided to allow travelers to click on the entire
Fetching Data from the Dom I //Goal: Create a discount variable, and then assign the discount that is stored in the data() attribute on the .tour element. To assign the correct data value, traverse from the clicked button, $(this), to the closest .tour element, and then use data() to find the discount. We don't need to do anything with the discount variable just yet.