views:

1306

answers:

2

This bit of HTML and Javascript works in IE6, FF2 and FF3. I can't find any reason why it shouldn't work in IE7 also, but this.selectedIndex always returns 0.

** in javascript file
function onTypeChange()
{
    alert($('type_id').selectedIndex);
    if ($('type_id').selectedIndex != 0) 
    { 
        Element.update('chosenType', this.options[this.selectedIndex].text);     
        Form.Element.enable('go_button'); 
    } else {
        Element.update('chosenType', 'Selected Type'); 
        Form.Element.disable('go_button');
    }
}

** in html
<select class="hosp_select_buttons selectbox" id="type_id" name="type[id]" 
        onchange="onTypeChange();">
<option value="">Please select</option>
<option value="1594">Ambulatory Surgical Center</option>
<option value="1595">Birthing Center</option>
<option value="1596">Comprehensive Outpatient Rehabilitation Facilities</option>
<option value="1597">Drug Abuse Treatment Program</option>
<option value="1598">Mammography</option>
<option value="1599">Narcotic Treatment Program</option>
<option value="1600">Outpatient Physical Therapy</option>
<option value="1601">Private Home Care Provider</option></select>

** Edited to change the stylistic things people objected so strongly too. The alert still says the selectedIndex is 0 after I change the select box. This code has, and still does work in all browsers other than I.E. 7

+3  A: 

You're trying to get selectedIndex from the option list.

Use this.selectedIndex instead of this.options.selectedIndex.

Also see this example for cleaner usage: http://www.mredkj.com/tutorials/tutorial002.html

Steven Richards
That was what I had at first, it didn't work either. I've been trying all kinds of things, including getting the select tag explicitly through $('type_id') and this.selectedIndex instead of this.options.selectedIndex
Kyle Boon
Interesting. That should definitely work. So if you simply use: onchange="alert(this.selectedIndex);" you don't get the correct result? I don't have IE7 installed, but IE8 and the IE7 compatibility mode show the correct result. You might want to try disabling Prototype to see if that's interfering.
Steven Richards
Just tried the example page I linked to in IE7 in VPC and it worked there. Note that their method is using document.getElementById().
Steven Richards
This code clearly should work - I'm trying to find some sort of weird conflict in jquery or prototype
Kyle Boon
+1  A: 

I know this is old, but I can't help seeing it here unaswered. I think the problem here is that you're usign $('type_id'), which returns an Element in jquery (I believe). To access the actual HTML element you have to use $('type_id').dom or something like that. I think if you use document.getElementById('type_id') it should work.

derickito
I wish I still had the code to test this out for sure but I believe this is the correct answer.
Kyle Boon