Daniel Kats
Daniel Kats

Reputation: 5564

How to trigger an event in an input field changed with Javascript

REWRITE: I have a select field with an associated onchange event.

<select id='customer' onchange='loadRate(this.value)'>

At some point in my code, I assign a value to this select field with Javascript.

document.getElementById('customer').value = "Main St Packaging";

Why does this not trigger the onchange event? How do I fix it so that it does? Right now I am doing it by writing explicitly:

loadRate('Main St Packaging')

but I was wondering if there is a better way?

Upvotes: 3

Views: 6113

Answers (1)

maerics
maerics

Reputation: 156662

Try calling the "onchange" method explicitly:

var el = document.getElementById('customer');
el.value = "Main St Packaging";
el.onchange(); // Will run "loadRate(this.value)", per your HTML.

Upvotes: 2

Related Questions