5

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?

Peter Ajtai
  • 56,972
  • 13
  • 121
  • 140
Eureka
  • 63
  • 1
  • 1
  • 4
  • 2
    What is this bucle you talk about? – Matti Virkkunen Sep 22 '10 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 Sep 22 '10 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 Sep 22 '10 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 Sep 22 '10 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 Sep 22 '10 at 21:57
  • Doesn't matter. You made me laugh xd – Eureka Sep 22 '10 at 21:57
  • nope, or at least I don't know it. All the info is here: http://code.google.com/apis/ajaxlanguage/documentation/index.htm (Section Translation, not translitaration) – Eureka Sep 22 '10 at 22:01
  • @Matti Virkkunen, @Pointy: Sorry I didn't realize that 'bucle' wasn't the word in English. I meant Loop. Bucle=loop in spanish. Sorry. Fortunately, I get the answer. – Eureka Sep 22 '10 at 22:15
  • @Eureka you're a good sport, and your English is about 10000% better than my Spanish :-) Also "buckle" really is a tasty dessert. – Pointy Sep 22 '10 at 22:25
  • @Pointy I didn't know it. New word for my English vocabulary. Thanks! ;) – Eureka Sep 22 '10 at 22:48

3 Answers3

9

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 1
    I CAN'T. I do not know when I will get the answer and if I call the function inside the bucle it will call the google's API before it get the first answer – Eureka Sep 22 '10 at 21:49
  • How is it setting the value of the input if it doesn't wait until the result comes back? – Quentin Sep 22 '10 at 21:51
  • google.language.translate(text, "en", "fr", function(result) { document.getElementById('translation').value = result.translation; }); – Eureka Sep 22 '10 at 21:54
  • Js does this line and then continues with the next lines without the answer. Then later when the answer is given magically this line to set the value works. – Eureka Sep 22 '10 at 21:55
  • 3
    Well yes. It's a callback, a common pattern in event driven programming (which most JS is). You pass a function as an argument. That function is called when the data comes back. **Do the work in that function** – Quentin Sep 22 '10 at 21:59
  • Oh damm. Ok you're correct. I tried this before but different because the first time I do a translation inside this translation call and the results weren't good. Thank you very, very much. You got the answer to my question :) – Eureka Sep 22 '10 at 22:12
2

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.

Ivan Ferić
  • 4,725
  • 11
  • 37
  • 47
  • Is there someone to be fired only after change the value but not lost focus? – Eureka Sep 22 '10 at 21:50
  • Not that I know of. Maybe you could focus on something other than onchanged event for text input. Does the function that changes your text input do anything else? For example, maybe it calls submit form - if so you could listen to onsubmit event of the form element. – Ivan Ferić Sep 22 '10 at 21:58
  • The problem is that if I write some code to change something more it will be done before the text is written in the text box. Sounds absurd but... – Eureka Sep 22 '10 at 22:05
1

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'

Community
  • 1
  • 1
vol7ron
  • 40,809
  • 21
  • 119
  • 172