File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to access combo-box value in javascript?

 
Pras Tiwari
Ranch Hand
Posts: 186
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In my struts application inside my JSP page I am populating my select-box html:select using collection object in my form-bean. Now on onChange event of my selection-box I want to call javascript function & inside that javascript I want to display value of selected item from combo-box. How to do this?
 
Dom Lassy
Ranch Hand
Posts: 181
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<html>
<head>
<title></title>

<script>
function doChange(objSelect) {
alert(objSelect.value);
}
</script>

</head>
<body>

<select onchange="doChange(this);" name="mySelect">
<option value="1">One</option>
<option value="2">Two</option>
</select>

</body>
</html>
 
Shilpa Tendulkar
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<script language="text/javascript">
function findValue(frmObj){
myvalue = frmObj.form.myValues.options[frmObj.form.myValues.selectedIndex].value;
}
<html:form action="someaction">
<html:select property="myValues" name="MyActionBean" onchange="findValue(this);form.submit();">
<html ption value="1">1</html ption>
<html ption value="2">2</html ption>
</html:select>
</html:form>
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prashant,

In case you're confused by the two different approaches proposed by Dom and Shilpa, here's my two cents:

Both Dom's solution and Shilpa's solution work great as long as you know you have options that specify a value:

<option value="1">my option</option>

If you have options that do not specify a value as in:

<option>my option</option>

Only Shipla's solution works.

Note to Shilpa:

Thanks for the input. I hope you keep participating and contributing.

However, Java Ranch rules indicate you need two names (first and Last) for your display name. Please make sure the "publicly displayed name" in your profile meets the JavaRanch naming policy.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic