How to select value of dropdown with button? This can be accomplish with the simple function I wrote few days ago.
Lets start, you need to make a HTML form with a select box and a simple button. On this button you need to call a JavaScript function. In this js function you simply check whether all options of drop down match the value with button passed to function. If matches then it simply make drop down value selected.
<form name="myfrm">
<select name="test" id="test">
<option value="">Select Value</option>
<option value="test1">test1</option>
<option value="test2">test2</option>
</select>
<br />
<input type="button" name="mybutton" value="Click" onclick="javascript:myform('test2');"/>
</form>
Javascript function call on button click. Here is the definition. It checks in the loop for match value to all options of dropdown, if matches set selected.
<script language="javascript" type="text/javascript">
function myform(val)
{
var len=document.myfrm.test.options.length;
for(var i=0; i<len; i++)
{
if(document.myfrm.test.options[i].value==val)
document.myfrm.test.options[i].selected=true;
}
}
</script>
Enjoy it

Leave a Reply