tags:

views:

25

answers:

2

I have to update one element on page with Ajax, do I have to make changes to server side, or is it possible to load whole page with jquery and then update only for example one element?

A: 

Load will fetch the data from server and place the HTML in your element. So writing like this will update only element div1

$("#div1").load('http://yourserver/page.html');

See load

rahul
It will set the contents of div1 to the entire response text of page.html.If you want all of page1.html to go into div1 thats fine, but I believe newbie was wanting to request the SAME page that he had already loaded, bit suck only a small portion of the response out into an element, so that he need not create a new page containing just the fragment.
Michael Shimmins
+1  A: 

You don't have to, and it is possible, but I would argue that you shouldn't. One of the advantages of AJAX is that you are pulling a fragment off the server, and thus a smaller payload. If you're pulling the entire page again, it will execute all the database queries, many of which will be unrelated to your element.

You will also request all CSS/JS again, all text, etc.

I would make a separate page for the individual fragment and request that via AJAX and update the element with just the returned result.

However you can do it, and jQuery provides this through its load method:

http://api.jquery.com/load/

Loading Page Fragments

The .load() method, unlike $.get(), allows us to specify a portion of the remote document to be inserted. This is achieved with a special syntax for the url parameter. If one or more space characters are included in the string, the portion of the string following the first space is assumed to be a jQuery selector that determines the content to be loaded.

Michael Shimmins
+1 Agree wholeheartedly that this is against the principle of AJAX based architecture.
Sohnee