Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I refresh a DIV content?

<div id='here'></div> 

I am trying to refresh a certain div within a bunch of divs. The div content is basically generated by PHP along with data from a MySQL database in addition to many variables being sent with the help of an XMLHttpRequest.

The idea is to reload/refresh the div itself and not load a php file or to overwrite it with .text or .html. In this case, I cannot use .load('file.php')

What I have tried :

<script type='text/javascript'>     function updateDiv()     {          document.getElementById("here").innerHTML = document.getElementById("here").innerHTML ;     }  </script> 

and (for a every 3 second refresh) :

$(document).ready(function () {     setInterval(function () {         $('#here').load('#here'));     }, 3000); }); 

Ideally, I would require something like :

function reloadDIV () {document.getElementById("here").innerHTML.reload}  function reloadDIV () {$('#here').load(self)}  

so that I can use it in a onClick :

<a onclick='reloadDIV ();'>reload div</a> 
like image 869
cyberion1985 Avatar asked Nov 19 '15 10:11

cyberion1985


People also ask

How do you update a div in HTML?

Use the textContent property to change the text of a div element, e.g. div. textContent = 'Replacement text' . The textContent property will set the text of the div to the provided string, replacing any of the existing content. Here is the HTML for the examples in this article.

How do I reload a div without reloading the whole page?

Use this. $('#mydiv'). load(document. URL + ' #mydiv');

How do you refresh part of a page in HTML?

Use Ajax for this. Build a function that will fetch the current page via ajax, but not the whole page, just the div in question from the server. The data will then (again via jQuery) be put inside the same div in question and replace old content with new one. e.g.


2 Answers

To reload a section of the page, you could use jquerys load with the current url and specify the fragment you need, which would be the same element that load is called on, in this case #here:

function updateDiv() {      $( "#here" ).load(window.location.href + " #here" ); } 
  • Don't disregard the space within the load element selector: + " #here"

This function can be called within an interval, or attached to a click event

like image 187
Steve Avatar answered Oct 04 '22 11:10

Steve


For div refreshing without creating div inside yours with same id, you should use this inside your function

$("#yourDiv").load(" #yourDiv > *"); 
like image 24
Олег Шилюк Avatar answered Oct 04 '22 12:10

Олег Шилюк