最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Input element's value changed: do something - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Oct 31, 2010 at 21:07 einsteineinstein 13.9k29 gold badges86 silver badges110 bronze badges 5
  • Couldn't you make your value-modifying code fire the listener manually? – thejh Commented Oct 31, 2010 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 Commented Oct 31, 2010 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 Commented Oct 31, 2010 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 Commented Oct 31, 2010 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 Commented Oct 31, 2010 at 21:25
Add a ment  | 

1 Answer 1

Reset to default 6

Try this:

inputTitle.addEventListener("change", function(){});
发布评论

评论列表(0)

  1. 暂无评论