Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Onclick doesn't work in Chrome

    <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?

like image 252
good_evening Avatar asked Mar 01 '11 22:03

good_evening


People also ask

Why Onclick is not working on option tag?

Your handler should be on your select element, not on the option elements. Try adding an onChange handler on your select element and reference the selected dropdown item on the events target. value property in your handler.

How do I get Onclick to work?

The onclick attribute is an event attribute that is supported by all browsers. It appears when the user clicks on a button element. If you want to make a button onclick, you need to add the onclick event attribute to the <button> element.

Does Onclick only work once?

addEventListener can add multiple events to a particular element. onclick can add only a single event to an element.

Can you use onclick on a link?

This is a type of JavaScript link - the onclick attribute defines a JavaScript action when the 'onclick' event for the link is triggered (i.e. when a user clicks the link) - and there is a URL present itself in the onclick attribute.


2 Answers

<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.

like image 51
Zoidberg Avatar answered Oct 13 '22 05:10

Zoidberg


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.

like image 45
glomad Avatar answered Oct 13 '22 03:10

glomad