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 jQuery Basics (2014) Introduction to jQuery Ways to Include jQuery in a Project

Why myCode() doesn't work?

I still do not understand why at min 2.45 the function does not work!

I don't like the way Andrew teachs and i think he's getting much confusion in the head of the students!!

Is there someone who understood why it doesn't work and can explain me?? Thanks!!

8 Answers

Hey Luca,

Can you post your current code, please?

Hi Jacob,

this is the code:

function myCode() {
  $(".warning").hide().show("slow");
}

myCode(); 

But that's the code Andrew wrote in the lesson "Ways to Include JQuery in a project" in the course "JQuery Basics"

Thanks a lot

Hey Luca,

Can you explain what is happening for you on page load?

You should see, "It's a trap!" move from the top left of the page to the top center of the circle illustration.

I've just tested the code you posted and it works on my end.

Just to confirm, are you using Workspaces or are you doing this through a text-editor off-site?

Jacob,

you should look the video :-)

I just do not understand what Andrew is talking about about this code because in the video he says that the code doesn't work! And i do not have clear the connection with what he's speaking about after this example

Hey Luca,

If I'm not mistaken, the only part of the video where Andrew refers to something not working is regarding the placement of the script tags. If you place your two script tags at the top of your index page, your JavaScript will load before the content inside of your body tags load. If you place the script tags before the closing body tag, users will see the content load before the JavaScript loads, which is considered best practice. And because of this placement, you no longer need to place your jQuery code inside of a ready method because the DOM has already loaded. Does that make sense?

If Andrew mentioned something else, please mention the video time marker. I'm on the go at the moment.

Hi Jacob,

at minute 2.51 he says that it doesn't work, even if on me it works! So i actually do not understand what is he refferring to! And i also do not understand what is the explanation he makes after that and ehy he makes it.

Thanks a lot Jacob!!

Hey Luca,

He is referring to what I just tried to explain.

Did you continue with the video until he explains $(document).ready();?

If you did, you should have:

function myCode() {
  $(".warning").hide().show("slow");
}

$(document).ready(myCode);

But towards the end of the video, this will no longer be necessary.

Read the documentation for ready and see if it makes sense: link

Ehi Jacob,

yes i just finished watching the video. But i do not understand why he said that it doesn't work if instead it works?? As i can see from the video, the code works regardless of the code is at the top or at the bottom of the code. Isn't it?

Hey Luca,

I'm at work right now but will get back to you within a few hours. I apologize for the delay.

Hi Jacob,

any news??

Hey Luca Di Pinto, the code at that point should not work because the script tag is placed inside the head tag in the document, since javascript is being loaded before the content of <body></body> the script is not selecting the warning class (because it has not been loaded yet) hence you should not see the animation... If what you said is correct and you are able to see it working, check where your script tag is located(most likely at the end of the body tag which is why you see it working)... Remember that in order for JavaScript||JQuery to select your elements, they should be loaded first, which is why we have the $(document).ready() function, this is telling JQuery to wait until the DOM is completely loaded before it runs... Hope this helps!

Keith Carrillo
Keith Carrillo
6,909 Points

Can anyone help me see why the code isn't working.

<!DOCTYPE html> <html> <head> <title>Take Evasive Action</title> <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" title="no title" charset="utf-8">

</head> <body> <p class="warning"> <span>It's A Trap!</span> </p> <div class="image"> <img src="img/ackbar.gif" /> </div>

<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>

<script src="js/app.js" type="text/javascript" charset="utf-8"></script>

</body> </html>