# Tuesday, July 24, 2007
Tuesday, July 24, 2007 10:01:33 PM UTC
« [Previous Post] Going Quadcore The Quadcore Has Lande... [Next Post] »

This has had me stuck for the past hour. Give me a break. I really hope this is just a bug in the “beta” of Safari.. because it’s extremely dumb.

This Javascript method takes a passed in SELECT element (via ID) and the value you want selected, and will loop through all of the SELECTs options, and select the one whose value matches:

function selectOptionListValue(psElementId, psValue) {
    var OptionList = document.getElementById(psElementId).options;
   
    for (var i = 0; i < OptionList.length; i++) {   
        if (OptionList[i].value.toLowerCase().trim() == psValue.toLowerCase().trim()) {
            OptionList[i].selected = true;
            break;
        }
    }   

                       
    return;
}

Looks simple, right? It kinda is. Works great in Opera, IE, FireFox, etc… but in Safari, nothing happens. Your SELECT gets nothing selected. It turns out, you need to specify the SELECT’s selectedIndex in order to get it to work properly— adding this line before the “return;” fixes it on Safari:

OptionList.selectedIndex = i;

Talk about retarded. Any “Safari developers” out there want to throw in their two cents?

 
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, strike, strong, sub, sup, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview