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.



<html>
<head>

<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;
}
</script>

</head>

<body>

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">kuppiya.com</option>
<option value="2">kusal</option>
</select>

</body>
</html>

No comments:

Post a Comment