3

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?

Community
  • 1
  • 1
Gargob
  • 251
  • 4
  • 14

2 Answers2

3

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")
});
Vytautas Butkus
  • 5,365
  • 6
  • 30
  • 45
  • 1
    There's no such thing as a "change input" event. There is `change`, but that only gets fired when the input is blurred. – Michael Mior Dec 11 '12 at 13:04
  • If you don't know better dont say anything. You can check it on google. Only problem that input event is not supported in IE<9 – Vytautas Butkus Dec 11 '12 at 13:07
  • @VytautasButkus - Please provide a link. I'm not finding anything on google. – broofa Dec 11 '12 at 13:12
  • uhm, yeah, that's the `change` event that @MichaelMior referred to. `change input` isn't a real thing. – broofa Dec 11 '12 at 13:14
  • 1
    @broofa Wow, now you are really stupid. I give you link with information about event existance. You can event try it on Stackoverflow(just open console) $("input").first().on("input", function(){console.log("i really work")}) And you still speak nonsense? Change event doesn't capture paste'ing or draging text into box and input captures it all. Check before speaking. – Vytautas Butkus Dec 11 '12 at 13:19
  • Just realized that the confusion here is that jquery will accept a space-separated list of event names for it's first argument. – broofa Dec 11 '12 at 13:38
  • 2
    @VytautasButkus You're absolutely right, so thanks for the clarification. But such a hostile tone and calling people stupid is not welcome on SO. – Michael Mior Dec 11 '12 at 13:40
  • I'm really sorry for that(my apologies @broofa) but just couldn't hold my self after I give reference and people say that it's nonsense :) – Vytautas Butkus Dec 11 '12 at 13:42
  • @VytautasButkus Thanks :) My initial remark wasn't meant to be insulting, but it's hard to be concise in writing and not come off sounding a little rude. I had completely forgotten about space delimited events in jQuery! – Michael Mior Dec 11 '12 at 13:44
0

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
});
Michael Mior
  • 28,107
  • 9
  • 89
  • 113
  • doesn't capture subtle forms of changes, such as user selecting a substring and using mouse to drag selection to different part of input. – broofa Dec 11 '12 at 13:03