Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Jonathan Dunsby
2,120 PointsVideo: Including jQuery in our project
Hi
In this video the aim (as far as I understand) is:
- Add an "Order Now" button before each Paypal order form.
- Hide the order form
- When clicked, the "Order Now" button should be hidden and the order form should be shown (slowly).
- Disable the "Order Now" button from following its href link.
This is the code that we end up with at the end of the video:
<script type="text/javascript">
$(".order_form").hide().before("<a href='#' class='order_now'>Order Now</a>");
$(".order_now").click(function(){
var $link = $(this);
$link.next().show("slow").prev().remove();
return false;
});
</script>
Questions
If we are disabling the "Order Now" button as a link then why do we make the button a link element to begin with?
Why do we need to declare $(this) as a variable?
Could the .hide() method be used instead of the .remove() method?
Thanks
4 Answers
Andrew Chalkley
Treehouse Guest TeacherHi Jonathan,
We're using progressive enhancement to enhance the page when people have JavaScript enabled. Because the element would be defunct on a page with JavaScript switched off we need to dynamically add it in rather than having it already in in the existing HTML.
We don't need to. We did it just for clarity. You could write
$(this).next().show("slow").prev().remove();all on one line.Try it out :)
Hope those answer your questions :)
Regards Andrew
Jonathan Dunsby
2,120 PointsThanks for the reply Andrew Chalkley =] (and the lessons!!)
I understand the reason for adding the element dynamically. In this case the div seems to be styled differently to the anchor in CSS. If this were not the case would it be more efficient to add the element dynamically as a div rather than an anchor so that we don't need the extra code of return false;?
Ahh that makes sense I suppose =]
I used $(this).hide().next().show("slow"); which seems to do the trick. Is there a reason the .remove() method would be more suitable?
In theory would using less methods cause a page to load any faster?
I feel I'm probably nitpicking but I'm just curious =]
Thanks
Andrew Chalkley
Treehouse Guest TeacherHi Jonathan,
- You could add it as a
divand add another line of CSS tocursor: pointerbut returning false has more of value in learning about stopping default browser behaviour than styling. - We showed how to
hideandshowadd elements to the DOM, needed to show removing :)
As for speed it'd be negligible on a site this size. But yes on bigger complex sites you want to keep an eye on the amount of code you're writing.
Regards Andrew
Jonathan Dunsby
2,120 PointsThanks for the answers Andrew.
I get it now! =]
J