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) Creating a Simple Lightbox Perform: Part 2

adding from jQuery doesn't work

Hi

So I've typed into my js file the following:

$("body").append(' <p> I am here </p> ');

But my webpage doesn't show any change. The console shows no p elements. Any ideas why?

I dont know why this post doesn't show p elements but the statement 'I am here' is wrapped in them!

2 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Are you sure you've linked jQuery correctly?

For jQuery to work it needs to be linked to the jQuery library with some code like this,

<script src="code.jquery.com/jquery-1.10.2.min.js"></script>

At the bottom of your page. Log on to jquery.com to find the latest version of jQuery. :-)

You need to write jQuery inside of a 'ready' function. Your code is trying to find the 'body' div but your html isn't actually loaded yet. Try this:

$( document ).ready(function() { $("body").append(' I am here '); });

The .ready() function fires when the HTML is loaded into the DOM.