Adding listener to div tag to view innerHtml content

I have a div tag and I am changing its innerHtml attribute. And I want to add a listener to this div tag to show / hide when its innerHtml has changed.

+2


a source to share


3 answers


You can use setInterval

to periodically check the content and act accordingly, for example:



function innerHTMLChanged() {
    if$("#myDiv").html() != "Some HTML for comparison, perhaps the previous state?") {
        $("#myDiv").hide();
    } else {
        $("#myDiv").show();
    }
}

// fires every half a second
setInterval(innerHTMLChanged, 500);

      

+1


a source


There is no such thing as a "listener" for an attribute innerHtml

. Whatever you do (but shouldn't), there is a timer that checks if the value has changed since the last update. Please, do not do that.



+3


a source


You are not using a function onchange

. JavaScript doesn't have it inline yet. But there are many code examples for google for you.

-1


a source







All Articles