Gargob
Gargob

Reputation: 251

Detecting when DOM has change value of input continuously

So, I have an input bar that I want to continuously search if it's been modified. I checked some stackoverflow questions like Detecting DOM change events and I checked this DOMSubTreeModified thing.

It works the first time, but I need to continuously check if there's been an update. Is there a way to loop it?

Upvotes: 3

Views: 2555

Answers (2)

Michael Mior
Michael Mior

Reputation: 28753

Are you talking about a change in value of an input tag? In that case, you're not talking about the DOM being changed. Try the onKeyDown event handler.

document.getElementById('input').addEventListener('keydown', function() {
  // Perform search
});

Upvotes: 0

Vytautas Butkus
Vytautas Butkus

Reputation: 5535

You can use timeintervals:

var oldVal = document.getElementById("yourInput").value;
function check(){
    if(document.getElementById("yourInput").value !== oldVal){
        alert("value changed");
    }
}

setInterval(check, 1000);

Code should be self explanatory :)

Or you could bind change or input events (but they should not work if you change value with javascript). Sorry using jQuery in this example :)

$("#yourInput").on("change input", function(event){
    alert("value changed")
});

Upvotes: 3

Related Questions