[Javascript] setting select drop selectedIndex

Peter Brunone peter at brunone.com
Wed Aug 2 14:21:14 CDT 2006


		   Why would you want it to die just because the browser isn't up to date, when there are perfectly valid methods for finding the control through the form object?

Peter

				From: Paul Novitski paul at juniperwebcraft.com

>At 10:21 AM 8/2/2006, Schalk wrote:
>>Should the following be able to set the selectedIndex of a select drop down?
>>
>>document.checkout.billingstate.options[document.checkout.billingstate.selectedIndex].value 
>>= state;

At 10:30 AM 8/2/2006, Paul Novitski wrote:

>No, that statement sets the value of the option at the selected 
>location. I believe what you want is:
>
> document.checkout.billingstate.selectedIndex = state;
>
>where 'state' is an integer 0-N.

I prefer to code this as:

 ... // exit silently if it's not a DOM-enabled user agent if (!document.getElementById) return; // find the SELECT list var oList = document.getElementById("billingstate"); // might as well abort if the markup's wrong if (!oList) return alert("Fatal HTML error: billingstate not found"); oList.selectedIndex = state;This uses DOM methods and lets the program exit gracefully instead of simply crashing if everything isn't as it should be.Paul _______________________________________________Javascript mailing listJavascript at LaTech.eduhttps://lists.LaTech.edu/mailman/listinfo/javascript
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20060802/836364d1/attachment.htm>


More information about the Javascript mailing list