Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Script problem

 
Shanmugam Muthukumarasamy
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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" />
</html:select><br>

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;
alert(text);
//alert(document.forms[0].firstName.disabled);
document.forms[0].firstName.disabled = true;
//alert(document.forms[0].firstName.disabled);
}
</script>

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 ?

Thanks,
Shanmugam
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Eric
 
Shanmugam Muthukumarasamy
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric,

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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic