• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

how to access combo-box value in javascript?

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic