Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

75
Views
Execute JavaScript on div change

I have two scripts. One asks my backend about new data in database and if there is something new, add this to div with id 'box'. In second script I want to monitorize this and if something is changed in this div, I want to call my function. How to do that?

$(document).ready(function(){
            refreshTable();
        });
        function refreshTable(){ //loading data to div
            $('#messbox').load('getData.php', function(){ setTimeout(refreshTable, 1000); });
        }
        function scroll(){ //function to call
            $("#messbox").scrollTop($("#messbox")[0].scrollHeight);
        }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use a MutationObserver to listen for changes in the element's childList:

const targetNode = document.getElementById('target');

const callback = function(mutationsList, observer) {
    for(const mutation of mutationsList) {
        if (mutation.type === 'childList') {
            myFunction()
        }
    }
};

const observer = new MutationObserver(callback);
observer.observe(targetNode, {childList: true});

function myFunction(){
  console.log("Change!")
}
<div id="target"></div>

<button onclick="target.innerHTML += '<p>Hello World!</p>'">Add something to #target</button>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!