Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript event that fires without user interaction?

A textbox on my form may change depending on what's selected in various drop down lists.

Is there a way to call a javascript function when the textbox value changes?

Tried onchange event but this only seems to work if a user manually changes the textbox value.

Cheers,

Breandán

like image 548
Breandán Avatar asked Jan 27 '26 22:01

Breandán


2 Answers

No, javascript-triggered changes to form elements don't trigger events. If they did, this would cause all sorts of recursive infinite loops.

The simple solution is to call your onchange function manually whenever you change the value of the textbox, however you could also use some kind of wrapper function. As a very basic example:

function updateTextField(new_text) {
  text_field.value = new_text;
  text_field.onchange();
}
like image 141
Gareth Avatar answered Jan 29 '26 11:01

Gareth


When setting the content of the text box from your JS code, call out to another function passing in the text to set, you can then call it from where ever you need and do your logic then.

like image 29
Kieron Avatar answered Jan 29 '26 12:01

Kieron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!