Home >Web Front-end >JS Tutorial >How to Dynamically Reload the Contents of a Specific Div Using jQuery/AJAX?
Reload Content in Div using jQuery/AJAX
Requirement: Reload the contents of a specific div upon button click, without refreshing the entire page.
Code Snippet:
Consider the following code:
<code class="html"><div id="step1Content"> <div id="list"> <!-- Content to be reloaded --> </div> </div> <input type="button" id="getCameraSerialNumbers" value="Capture Again" /></code>
<code class="javascript">$("#getCameraSerialNumbers").click(function() { $("#list").load(location.href + " #list"); });</code>
This code uses jQuery's load() method to reload the contents of the #list div. The location.href represents the current page's URL, and adding " #list" to it specifies that only the portion of the page contained in the #list div should be loaded.
Note: The space between the URL and the second "#" is crucial. Without it, the entire page will be loaded into the #list div instead.
Detailed Explanation:
This approach allows you to dynamically update the content of a specific div on a web page without refreshing the entire page, providing a better user experience and maintaining the current page state.
The above is the detailed content of How to Dynamically Reload the Contents of a Specific Div Using jQuery/AJAX?. For more information, please follow other related articles on the PHP Chinese website!