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, how do you load in Content without Refreshing?

Hi, I literally just started JQuery, so there are things I'm still figuring out. I found a site about my question but I don't quite understand the solution. This is the site --->

https://code.tutsplus.com/tutorials/how-to-load-in-and-animate-content-with-jquery--net-26

this page was posted in 2008, but unfortunately I cant find a more up-to-date solution. I understand the methods he uses. what I don't understand is

var toLoad = $(this).attr('href')+' #content';

Q. Why did he create this variable with the added +' #content' if I just want to load from the current href?

function loadContent() {
    $('#content').load(toLoad,'',showNewContent())
}

Q. What are the ' ' for?

I tried this code snippet he posted at the end but I cant get it to work.

1 Answer

Steven Parker
Steven Parker
243,318 Points

Adding the string " #content" to the URL creates a page fragment request. So instead of loading the entire URL contents, it will parse it looking for an element with the id of "content" and load only that element (with any child elements) into the target.

The second argument is data to be sent to the server with the request, and in this case it is a blank string. Since the second argument is optional and this doesn't appear to be of value, it could probably be omitted. It might be that earlier versions required the data argument to be present for a callback to be given.