2

As the title of my question states. whenever the value of an input element changes I want it to execute a function. I know that the function is working because I've been tested in onkey events like onkeyup. But, I don't wont to use onkey events because I have scripts that manipulate the input element's value. I also know that the problem lies in the first argument of the addEventListener method. I've tred various arguments like DOMAttrModified/change/DOMsomethingelse but it doesn't work. I've also looked at some similiar questions here in SO, JS Events: hooking on value change event on text inputs. The below code works for onkey events:

inputTitle.addEventListener('DOMAttrModified',function(event){
        someFunction(this.value);
},false);

Can someone pls help me. I Have tried to solve this problem for a long time now without success and I know that this is just a small one too. Pls give me some code example that is tested and working.

UPDATE: I've been wrong about one thing above. If I use the "change" event the function will be executed after it looses focus on the input element. So it does work. But I want it to work like onkeyup event, where the function is executed in realtime like Google instant. Unfortunenately, I can't use 'keyup' event beacuse I have som scripts that or say a button that are modifying the input element's value. When these scripts are run, the function is not executed.

UPDATE2: I figured out a work around this problem. And it is to add additional scripts to valemodifyings scripts that executes the "someFunction". I don't really like this solution, because it is not nice coding. I have maybe four scripts to calls that function

Community
  • 1
  • 1
einstein
  • 13,389
  • 27
  • 80
  • 110
  • Couldn't you make your value-modifying code fire the listener manually? – thejh Oct 31 '10 at 21:11
  • 1
    Why doesn't the `onchange` event work? It's fired whenever the focus leaves *and* the contents of the input field is changed. – Marcel Korpel Oct 31 '10 at 21:11
  • you can either use the `key` events (*as you stated*) or the `onchange` event, which is fired when the element looses focus (*after having changed its contents..*) – Gabriele Petrioli Oct 31 '10 at 21:13
  • I've tried inputTitle.onchange = someFunction(); but it didn't work. I also tried to modify the first attribute of the addEventListener method to 'change' or 'onchange' also didn't worked. On key events works but only half way. I have scripts that manipulates the input elements value. And when I do that the function someFunction above won't be executed. – einstein Oct 31 '10 at 21:22
  • Re: thejh I'm not that good in eventListener in JS can you be more specific of how to do that? – einstein Oct 31 '10 at 21:25

1 Answers1

6

Try this:

inputTitle.addEventListener("change", function(){});
Jochen
  • 1,853
  • 3
  • 20
  • 28