Preserve onchange for a dropdown list when setting the value with Javascript.

Posted by Zac Altman on Stack Overflow See other posts from Stack Overflow or by Zac Altman
Published on 2010-04-08T07:31:05Z Indexed on 2010/04/08 7:33 UTC
Read the original article Hit count: 369

I have a dropdown list with a piece of code that is run when the value is changed:

<select name="SList" onchange="javascript:setTimeout('__doPostBack(\'SList\',\'\')', 0)" id="SList">

Everything works fine when manually done. As an option is selected, the onchange code is called.

The problem begins when I try to change the selected value using a piece of Javscript. I want to be able to automatically change the selected option using JS, whilst still having the onchange code called, exactly as if done manually.

I try calling this:

form.SList.value = "33";

The right option is selected, but the onchange code does not get called. So then I try doing this:

form.SList.value = "33";
javascript:setTimeout('__doPostBack(\'SList\',\'\')', 0);

The right value is not selected and nothing happens.

FYI, the code is done in ASP.NET and Javascript.

What can I run to change the selected option whilst still calling the onchange code?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about select