File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes html select onchange Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "html select onchange" Watch "html select onchange" New topic

html select onchange

Ingoba Ningthoujam
Ranch Hand

Joined: Dec 04, 2006
Posts: 90
how to call a javascript function using html select onchange in struts?
function fun()
var id=document.getElementById("category").value
if (id != 0)

<html:from action="/" >
<html:select property="name" onchange="javascript:func()"/>
I am using Struts 1.2.9 so I cant put the name attribute in form.Pliz help me.
Nick Williamson
Ranch Hand

Joined: Jan 06, 2007
Posts: 73
try using this


if you have multiple forms on your web page you can go through them so if you have two forms on your page you can access th first one like above and the second form would just be using 1 instead of 0.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
In struts, the name of the form is always the name of the ActionForm bean to which the action is associated. Example:

<action name="myForm" path="/myAction" type="com.mycompany.MyAction" />

In the following form:

<html:form action="/myAction" />

The name of the form will be "myForm" and you can use it in any JavaScript function.

Hint: When writing JavaScript for a Struts JSP, remember it's the raw HTML generated by the Struts tags that is being used by JavaScript, and not the Struts tags themselves. If you ever have any doubt about what the generated HTML looks like for a given page, just use your browser's view source function to see it.

Consultant, Sima Solutions
Ingoba Ningthoujam
Ranch Hand

Joined: Dec 04, 2006
Posts: 90
Thank you. but it is not working.My code is
function fun1(id) { document.forms[0].action="/web/"+id return true; }

<td width="278"> <html-el:select property="category" value="" onchange="javascript:func1(this.value)"> <html-el ption value="select">Select Category</html-el ption> <logic:iterate id="id1" name="category" scope="session"> <html-el ption value="${id1}"><bean:write name="id1" property="value" /></html-el ption> </logic:iterate> </html-el:select> </td>

When select the option javascript error is highlighted.Please help.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
First of all, when writing JavaScript, you need to set your browser so that it shows detail on all JavaScript errors. The setting is different for the various browsers, but look for it - you'll find it. Then tell us the specific JavaScript error you're getting.

Here's one error I've spotted: This statement:

needs a ";" before "return true".
I agree. Here's the link:
subject: html select onchange
jQuery in Action, 3rd edition