views:

1721

answers:

3

Why isn't the following piece of code working in IE8?

<select>
    <option onclick="javascript: alert('test');">5</option>

Quite bizarre - no alert is shown in IE8. I do not see the error icon in the left corner as well. Of course it works in FF and Opera. Any ideas?

+15  A: 

Putting an onclick handler on an <option> element seems.... weird to me. You might want to switch that to the more common onchange event of the <select>. You can still do whatever you want to do from there, and this is the "accepted" way of doing whatever you want to do to the select. That being said, you might want to try removing the javascript: part of it. That is only needed when you are executing Javascript in a link href for example. An onclick handler expects javascript.

Paolo Bergantino
thanks, it helped
Petrunov
Actually, I've found out that it's a bit more complicated than that in IE. For example, if you've only had VBScript code in your page before that, it expects VbScript code here as well. If there is both JScript and VBScript code, then it was important which one was the first one in the page.
Vilx-
People still put VBScript in their pages?
Paolo Bergantino
+2  A: 

I have you tried just:

onclick="alert('test');"

Pretty sure you don't need the javascript: prefix.

jeffamaphone
+2  A: 

All versions of IE (6,7,8) do not support ANY event handlers on the option elements.

This is a (fairly) well known bug that the IE team has indicated they are in no rush to fix. :-(

Then again Opera, Safari & Chrome all have limited or no support for event handlers on options too.

Lack of events on options: bug 280

(related) Lack of styles on options: bug 281

scunliffe
IE ftl :(It creates too many problems.
the_drow