Follow Me

How to select value of dropdown with button

Thu, May 27, 2010

JavaScript

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 :)

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • blogmarks
  • Design Float
  • DZone
  • MySpace
  • Reddit
  • StumbleUpon
  • Twitter
, , , ,

Related Posts:


Recent Posts:

  • Best collection of firefox addons for web designers and developers
  • .htaccess basic features with example
  • how to include an external css and js with javascript dynamically
  • How to preserve line breaks in textarea mysql data
  • how to generate excel report with php and mysql
  • Leave a Reply