I'm trying to reload particular element on button on click using jQuery, but when I click the button, the entire page gets loaded in this element. I have tried the following code which loads the entire page in this element. I want to load only particular element.
<script>
$(document).ready(function() {
$("#button").click(function() {
$("#demo").load(location.href + "#demo");
});
});
</script>
</head>
<body>
<div id="example">
<p id="demo">hi</p>
<button id="button" type="button">press</button>
</div>
Use ” #id > *” With . load() to Reload a div in JavaScript. Use window. setInterval() to Refresh a div in JavaScript.
click(function() { location. reload(); }); The reload() function takes an optional parameter that can be set to true to force a reload from the server rather than the cache.
Refreshing part of a page periodically You can use the frame “reload” function periodically in frameset page itself. In the above code, “right_frame” reloads every second. setInterval() function is very closely related to setTimeout() – both have similar syntax: setInterval ( expression, interval );
You can use load()
with selector.
$("#demo").load(location.href + " #demo"); // Add space between URL and selector.
^
This will load only the #demo
element from the location.href
URL.
You have to load the page using $.get()
and extract the #demo
fragment:
$("#button").click(function() {
$.get(location.href).then(function(page) {
$("#demo").html($(page).find("#demo").html())
})
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With