Home > Web Front-end > JS Tutorial > body text

How to Update a Web Page Section Using jQuery/AJAX Without Reloading?

Susan Sarandon
Release: 2024-10-20 16:22:29
Original
358 people have browsed it

How to Update a Web Page Section Using jQuery/AJAX Without Reloading?

How to Update a Portion of a Page Without Reloading using jQuery/AJAX

Reloading a specific div on a button click without affecting the entire page is a common requirement in web development. This can be achieved using jQuery's AJAX functionality.

Example

Suppose you have a button that, when clicked, should update the content of a div called div1. Here's how you can do it:

HTML:

<code class="html"><input type="button" id="myButton" value="Update DIV" />

<div id="div1">
    <!-- Content to be updated -->
</div></code>
Copy after login

jQuery:

<code class="javascript">$("#myButton").click(function() {
    $("#div1").load(location.href + " #div1");
});</code>
Copy after login

Explanation:

  • The jQuery click event handler is attached to the button with the id myButton.
  • When the button is clicked, the load() function is called on the div with the id div1.
  • The load() function loads the content of the specified URL into the div.
  • In this case, the URL is location.href " #div1". This retrieves the current page's URL and appends the #div1 selector.
  • The space before the second # sign is crucial. Without it, the above code will reload the entire page within the div.
  • As a result, when the button is clicked, the content of div1 will be updated with the content of the same div from the current page.

Note: Remember to include the jQuery library before using the above code.

The above is the detailed content of How to Update a Web Page Section Using jQuery/AJAX Without Reloading?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!