Thursday, February 5, 2009

How to redirect a page in Dropdown (select) onchage

Sometimes you need to redirect or refresh a page when you select a new option in a dropdown box (select box) with some url parameters (query string).
Look at following code and try it yourself.


<title>DropDown Redirect OnChange</title>

<script type="text/javascript">

function chng_page()
get the selected option index
it start with 0
var select_index = document.getElementById('names').selectedIndex;
// use alert(select_index); see the value

//get the value of selected option
var option_value = document.getElementById('names').options[select_index].value;

//redirect with value as a url parameter
window.location = 'page.php?id=' + option_value;



we are using onchange event to
call a javascript function
<br />
<select name="names" id="names" onchange="chng_page()">
<option value="0">select</option>
<option value="1"></option>
<option value="2">kusal</option>


No comments:

Post a Comment