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?
views:
25answers:
2Load 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
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:
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.