Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8073363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:19:16+00:00 2026-06-05T14:19:16+00:00

Right now im trying to find a way to detect when an elements HTML

  • 0

Right now im trying to find a way to detect when an elements HTML has changed.

I’m currently trying:

var a, b;
setInterval(function() {
    a = $('#chat').text();
}, 150);
setInterval(function() {
    b = $('#chat').text();
    if (a !== b) {
        alert("There has been a new message.");
    }
}, 200);​

What I do is every 150 milliseconds I check for the HTML of #chat and then every 200 seconds I check the HTML again and then check if variable a does not equal to variable b them in the future I will so something with that but for right now I just alert something.

You can see it live here: http://jsfiddle.net/MT47W/

Obviously this way is not working and is not very accurate at all.
Is there a better/different to do/achieve this?

Thanks for any help, I’ve been trying to figure out how to do this a better for about a week now but I just can’t find a fix for this and i’m hoping I posted this problem at the right place, and at the right time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:19:17+00:00Added an answer on June 5, 2026 at 2:19 pm

    Use a var to store the element’s current text then check against it in a setInverval and update the var to store the current text after checking:

    var a = $('#chat').text();
    setInterval(function() {
        if (a !== $('#chat').text()) { //checks the stored text against the current
            alert("There has been a new message."); //do your stuff
        }
        a = $('#chat').text(); //updates the global var to store the current text
    }, 150); //define your interval time, every 0.15 seconds in this case
    

    Fiddle

    You may as well store the value in the .data() of the element to avoid using globals.

    Example using .data():

    $('#chat').data('curr_text', $('#chat').text());
    setInterval(function() {
        if ($('#chat').data('curr_text') !== $('#chat').text()) {
            alert("There has been a new message.");
        }
         $('#chat').data('curr_text', $('#chat').text());
    }, 150);
    

    Fiddle

    Another approach, to save client’s memory, you can just store the number of child divs your #chat element has:

    $('#chat').data('n_msgs', $('#chat').children().length);
    setInterval(function() {
        if ($('#chat').data('n_msgs') !== $('#chat').children().length) {
            alert("There has been a new message.");
        }
         $('#chat').data('n_msgs', $('#chat').children().length);
    }, 150);
    

    Fiddle


    EDIT: Here’s my very final addition, with a DOM mutation event listener:

    $('#chat').on('DOMNodeInserted', function() {
        alert("There has been a new message.");
    });
    

    Fiddle (not tested in IE < 8)

    Note: As noted in the comments, even though mutation events are still supported they’re classed as deprecated by W3C due to the performance loss and some incompatibilities across different browsers, therefore it’s suggested to use one of the solutions above and only use DOM Mutation events when there’s no other way around.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My application has a very simple model right now and I'm trying to find
Right now I'm trying to write a function to moving up frame when keyboard
I'm in a bind right now trying to find the source of a problem.
I'm taking a C# class right now and I'm trying to find out the
Hey guy's i'm making a Paddle/Pong game and right now i'm trying to find
Right now I'm trying to create my own tiny MVC (just for practice and
Right now I'm trying desperately to get @font-face to work in my website. This
Right now I'm trying this: int a = round(n); where n is a double
Right now I'm trying to set the default audio device on Windows 7 programmatically,
Right now I have been trying to use Launchpad's API to write a small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.