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

javascript - Why onchange doesn't work? - Stack Overflow

programmeradmin0浏览0评论

I'm just looking for an answer on the net but I haven't reached any one. I'm playing with google's api translation and I have a problem with JS. In fact my code is 100% ok except one operation.

My problem is that the translation is not given at the time. After js sends the information to google it doesn't wait until the translation is given. Instead of this it continues reading my code so it doesn't stop for get an answer. It proceess the information to be translated and then the answer will be get some time after. So because I want to do translations of translations I have done a sweet loop. Because I don't know how to stop js, I have done a page with diferent inputs text box and in my loop when the data is received it's send to the value of the text box and here is where it becomes the problem. I want to enter to my js function again when the text is changed so If js changes the value it will return to my loop when I get the translation. Simply and effective but if I put in the input box:

onchange="myfunction()"

the loop doesn't work. But the strangest is that if I manually change the text then the function works so it's as if onchange only works when a human change the text but not if js change the text. Is there any solution? May be some dinamic listener or something like that?

I'm just looking for an answer on the net but I haven't reached any one. I'm playing with google's api translation and I have a problem with JS. In fact my code is 100% ok except one operation.

My problem is that the translation is not given at the time. After js sends the information to google it doesn't wait until the translation is given. Instead of this it continues reading my code so it doesn't stop for get an answer. It proceess the information to be translated and then the answer will be get some time after. So because I want to do translations of translations I have done a sweet loop. Because I don't know how to stop js, I have done a page with diferent inputs text box and in my loop when the data is received it's send to the value of the text box and here is where it becomes the problem. I want to enter to my js function again when the text is changed so If js changes the value it will return to my loop when I get the translation. Simply and effective but if I put in the input box:

onchange="myfunction()"

the loop doesn't work. But the strangest is that if I manually change the text then the function works so it's as if onchange only works when a human change the text but not if js change the text. Is there any solution? May be some dinamic listener or something like that?

Share Improve this question edited Sep 22, 2010 at 22:14 Peter Ajtai 57.7k13 gold badges123 silver badges141 bronze badges asked Sep 22, 2010 at 21:31 EurekaEureka 631 gold badge1 silver badge4 bronze badges 10
  • 2 What is this bucle you talk about? – Matti Virkkunen Commented Sep 22, 2010 at 21:32
  • Might be worth looking at a framework such a jquery and using $(document) to process the js once the dom has loaded – Adam Hopkinson Commented Sep 22, 2010 at 21:33
  • 1 @Matti he says it's "sweet", so maybe he meant "buckle", which is a baked dish with fruit, kind-of like a "cobbler". When I'm doing lots of translations of translations, there's nothing I like more than to snack on some delicious blueberry buckle. – Pointy Commented Sep 22, 2010 at 21:49
  • I said "it's sweet" to say that does what i want correctly. It's not problem of this bucle, the problem is that I change the value of a text box by js and I want to call my function when the text box gets the new value – Eureka Commented Sep 22, 2010 at 21:53
  • Does the translation API not provide a callback function parameter to which you can provide a function to call when the translation has been done? – Russ Cam Commented Sep 22, 2010 at 21:57
 |  Show 5 more comments

3 Answers 3

Reset to default 10

The onchange event only fires if the user changes the value of the input. It isn't supposed to fire if the input is changed programmaticly.

Call the function from whatever function sets the value instead.

The onchange event is designed to fire when the user (and only the user) changes the value and for that reason it is fired only after the value was changed AND field lost focus.

Helpful Starters and that Bucle you speak of


  1. You should look at the onkeyup or onblur.
  2. If the input value is being set by JavaScript, then why not call the event after setting it?

I have no idea what a bucle is.
My best guess: it is Spanish for 'loop'

发布评论

评论列表(0)

  1. 暂无评论