11/11/2010

How to Get value of a select box in JavaScript

If you are using Id to get a select box value then it's little bit easier . If you don't have id in your select box then you need to use name attribute to select the select box. I have used name tag to get the select box value.

<script type="text/javascript">
function slectBox()
{
var aa= document.form.selectBox.selectedIndex;
var ab= document.form.selectBox.options[aa].value;
alert(ab);
}
</script>

<form action="" name="form">
<select name="selectBox">
<option value="1">one</option>
<option value="2">Two</option>
</select>
<input type="button" onclick="slectBox()" value="clickme" />
</form>

This is the example how it works.try it

1 comment:

  1. very cool & good tips for JS beginners, thank you very much for sharing.

    ReplyDelete