In this site, for example, there are 3 SELECT elements (Step 1, Step 2 and Step 3). I'm able to change the value of the first one with selectElement.value = selectElement.options[i].value;, but when I change it the Step 2 doesn't update it's options as it would if I would select it with the mouse. Is there any way to force it to update?
views:
46answers:
2The best way to do this is to have a function that updates the contents of the second select box, and call that function when you update the value of the first one. You'd then have a change handler (or a click handler, or a timer; depends on what you prefer for a UX) on the first select box that calls that function when its value changes. E.g. (in near-pseudocode):
<select id='box1' onchange='fillBox2();'>...</select>
<select id='box2'>...</select>
<select id='box3'>...</select>
function setValueInBox1(val) {
var box1 = document.getElementById('box1');
box1.value = val;
fillBox2();
}
So both the event and your logic call the same routine, fillBox2.
People routinely try to trigger the actual event handler of box1 (the change event in the above), but that can be difficult and error-prone. By having a common function that both the event handler and your logic can call, you avoid that issue.
If for whatever reason the above is impossible, if you're doing it specifically in IE and not worrying about cross-browser behavior (from your comment), you might look at the fireEvent method. For other browsers, you might look at the DOM dispatchEvent method.
Well , you could always invoke the method like
document.getElementById('x').onchange();
, where 'x' is the id of the select element