Abel Jojo
Abel Jojo

Reputation: 758

goto url using javascript on Select box | DropDown box

I have the below javascript function to go to the selected box value url.

function go(x) {
    alert(x);
    location = x.value;
}

I cannot use getElementById There may be more than 1 select box as the user differs I wrote a php to print all the selectbox inside a form and div

<div class="styled-select">
    <form name="menu">
        <select id=Admission onchange=go(this)>
            <option value=/admission>Add Existing Students</option>
        </select> 
        <select id=Student onchange=go(this)>
            <option value=www.bing.com>Student Details</option>
        </select>
    </form>
</div>

All suggestions are welcome.

Upvotes: 0

Views: 1614

Answers (1)

ThiefMaster
ThiefMaster

Reputation: 318488

You don't access the value of the element properly. Use this instead:

function go(x) {
    location = x.options[x.selectedIndex].value;
}

You also won't get an onchange event for a <select> with only a single option ever.

Upvotes: 3

Related Questions