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

JavaScript

Video: 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

  1. If we are disabling the "Order Now" button as a link then why do we make the button a link element to begin with?

  2. Why do we need to declare $(this) as a variable?

  3. Could the .hide() method be used instead of the .remove() method?

Thanks

4 Answers

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hi Jonathan,

  1. 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.

  2. We don't need to. We did it just for clarity. You could write $(this).next().show("slow").prev().remove(); all on one line.

  3. Try it out :)

Hope those answer your questions :)

Regards Andrew

Thanks for the reply Andrew Chalkley =] (and the lessons!!)

  1. 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;?

  2. Ahh that makes sense I suppose =]

  3. 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
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hi Jonathan,

  1. You could add it as a div and add another line of CSS to cursor: pointer but returning false has more of value in learning about stopping default browser behaviour than styling.
  2. We showed how to hide and show add 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

Thanks for the answers Andrew.

I get it now! =]

J