5
    <select class="input" style="width:200px">
        <option>---</option>
        <option onclick="window.location="link.php">one</option>
        <option onclick="window.location="link2.php"">two</option>
    </select>

It doesn't work in chrome, it work in opera/mozilla and so on. Any advice?

good_evening
  • 21,085
  • 65
  • 193
  • 298

4 Answers4

19
<select class="input" onchange="window.location.href = this.value" style="width:200px">
        <option>---</option>
        <option value="link.php">one</option>
        <option value="link2.php">two</option>
    </select>

I know its not EXACTLY the same... but having a click event on the option of a select list is not good practice. Instead of onchange, you could have onclick as well... but onchange really is the way to do this, in my opinion.

Zoidberg
  • 10,137
  • 2
  • 31
  • 53
4

Probably the safest approach is to use the onchange event of the select element, and use its value to determine the action to take. I don't think onclick works for option in IE, either.

glomad
  • 5,539
  • 2
  • 24
  • 38
3

Are the double quotes inside the onclick attributes not meant to be single quotes ?

Alain Pannetier
  • 9,315
  • 3
  • 41
  • 46
  • @Radek S: One thing I can think of is parser leniency (read: smartassery). – BoltClock Mar 01 '11 at 22:12
  • @BoltClock I can't really believe that Gecko is smarter than WebKit. Oh well, maybe it is. :) –  Mar 01 '11 at 22:14
  • @Radek. You're right, I typed a quick example with the quotes rights and it's like hey says. Works in FF, not in chrome. Thx for pointing this out. And Zoidberg's solution works fine. – Alain Pannetier Mar 01 '11 at 22:20
0

The onclick isn't going to work the way you're trying to get it to work, and you're going to have to account for differences in how the browsers deal with the select object. I would advise that you use the onchange event on the select object, then test for the selected option. Note that these events fire differently on different browsers.

Josh Anderson
  • 5,975
  • 2
  • 35
  • 48