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

Java Script problem

Shanmugam Muthukumarasamy
Ranch Hand

Joined: Jan 11, 2003
Posts: 83
Hi All,

I have the following snippet on my JSP

<html:select property="priority" on change='javascript isableAddress()'>
<html ptions collection="namedInsuredPriority" property="value" labelProperty="name" />

and my Javascript function is

<script language='javascript'>
function disableAddress(){
alert("I am here");
var text=document.forms[0].priority.options[document.forms[0].priority.options.selectedIndex].text;
document.forms[0].firstName.disabled = true;

When I change something on the priority dropdown, I am getting the alert of "I am here" but after that I am getting the javascript error on the page as

Error: 'document.forms.0.priority.options' is null or not an object

Could any one guess what am I messing up here ?


SJCP 1.4<br />SCWCD 1.4<br />SCEA
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385
Well that is because the dropdown is probably not named priority, you need to do a view source on the page and see what it is called.

Shanmugam Muthukumarasamy
Ranch Hand

Joined: Jan 11, 2003
Posts: 83

Thanks for the reply,
this is what I see in the Viewsource

<select name="priority" onchange="disableAddress(this.form)"><option value="" selected="selected">Select...</option>
<option value="First">First Named Insured</option>
<option value="Additional">Other Named Insured</option></select>
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
You could also get this message if there is any other input element named "priority" anywhere in the form.

Also, the selectedIndex property is a property of the select control, not of the options in the control. So, you should be writing:
var text=document.forms[0].priority.options[document.forms[0].priority.selectedIndex].text;

This would be a whole lot easier if you just pass the select object to the javaScriptFunction, something like this

<html:select property="priority" onchange="disableAddress(this)" />

The function would be:

function disableAddress(select) {
var text = select.options[select.selectedIndex].value;

Consultant, Sima Solutions
It is sorta covered in the JavaRanch Style Guide.
subject: Java Script problem
It's not a secret anymore!