views:

91

answers:

3

I have an onChange event on a select.

When I do an alert(this.value), it's working on Firefox, but not on Internet Explorer. Why not?

This is the code:

<select onchange="new Ajax.Updater('fiches', '/~project/index.php/folder/fiche', {asynchronous:true, evalScripts:true, parameters:'fiche=' + this.value});" class="input" id="fiche" name="fiche">
  <option value="0">Choisir ma fiche</option>
  <option value="1">Vous etes salariés</option>
  <option value="2">Sans emploi</option>
</select>
+1  A: 

Try this.options[this.selectedIndex].value.

Douwe Maan
I doubt this will help. `selectElement.value` will work all the way down to IE6 and maybe IE5.5. The problem is more likely to be the `this` reference.
Andy E
Afaik both `element.value` and `onchange="this..."` work in every browser, so I just posted this as a 'well, maybe IE's fcking up again, and this might work'. I don't see why it would work either, but given IE's habit of not working as expected, I guess it's worth a try.
Douwe Maan
A: 

It looks like you're using Prototype. Try calling $F(this) rather than this.value.

jeremy
A: 

I have had similar problems in the past with IE and selects. The value property of select items is a pain in IE. Usually the solution is to care about the selected option (not the select element) and access to its text attribute.

I'll do this in Jquery to access the text selected:

$("#my_select_item option:selected").text()

So, in raw javascript it should be something like:

document.getElementById("myselect").options[document.getElementById("myselect").selectedIndex)].text

or

document.getElementById("myselect").options[document.getElementById("myselect").selectedIndex)].value

That is the general idea to make selects + JS happen in IE.

nacmartin