3

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?

3
  • Can you not just make one function that updates all of the fields at once? Commented Jun 3, 2011 at 13:20
  • Well there are a lot of fields. That's basically what I did to get around this, but it seems pretty inefficient. Commented Jun 3, 2011 at 19:22
  • I'm not entirely sure if it's a duplicate, but the accepted answer seems relevant to your question: Trigger onchange event manually. Commented Jun 6, 2011 at 16:42

1 Answer 1

2

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.
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, JQuery for the win. I used $("#customer").change();
This throws an error "el.onchange is not a function", use el.onchange = <yourFunctionName> instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.