Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing URL through html select

What is the best way to change your URL through an html select?

<select>
<option selected="selected">Change to URL X</option>
<option>Change to URL Y</option>
</select>

What Javascript should be used?

like image 630
Daniel Kivatinos Avatar asked Sep 18 '09 15:09

Daniel Kivatinos


People also ask

How do I redirect to another page from the option selected?

This is done by using onChange event of dropdown listbox. Once the user selection one option then by onChange event we run one function SelectRedirect(). The function will receive the user selected option value like this. Here we have used getElementById to get the value of the selected option.

How do I change the selected value in HTML?

To change the selected option in an HTML <select> element we have to change the value property on the <select> element or the selected property on the <option> element we want to get selected.

How do you get select in HTML?

The <select> element is used to create a drop-down list. The <select> element is most often used in a form, to collect user input. The name attribute is needed to reference the form data after the form is submitted (if you omit the name attribute, no data from the drop-down list will be submitted).


2 Answers

<script type="text/javascript">
function navigateTo(sel, target, newWindow) {
    var url = sel.options[sel.selectedIndex].value;
    if (newWindow) {
        window.open(url, target, '--- attributes here, see below ---');
    } else {
        window[target].location.href = url;
    }
}
</script>

<select onchange="navigateTo(this, 'window', false);">
<option selected="selected" value="http://www.example.com/#X">Change to URL X</option>
<option value="http://www.example.com/#Y">Change to URL Y</option>
</select>

Some useful values of target might be 'window' (the current window) or 'top' (to break out of a frameset or iframe). If you want to open a new window instead, you could use navigateTo(this, 'someWindow', true);

The value of '--- attributes ---' is set using various properties as documented here for Mozilla and here for IE. For example:

'height=300,width=400,top=100,left=100,statusbar=0,toolbar=1'
like image 163
Grant Wagner Avatar answered Sep 19 '22 19:09

Grant Wagner


If you have jQuery you could do...

javascript:

$('#select_url').change(function(evnt){ location.href = $(this).val(); });

html:

...

like image 40
Stephen Avatar answered Sep 18 '22 19:09

Stephen