[selectedIndex] is undefined

Can anyone help!!!

Im currently designing a JSP, which has some javascript embedded. What i am trying to do, is disable a button, if a user selects a particular row from the drop down list.

At the moment, within the select tag, the value of a variable (called Status) is queried to find out which option should have the <option selected> tag by it.
The problem is every time onchange gets run, i get an error saying that ‘selectedindex’ is undefined.
Any ideas?



<% String status = DLPULGA3Bean.getStatus(); %>

function checkIfAll()
{

	if (window.document.DLPULGA3form.statusValue.options[selectedIndex].value == "All")
		{
	window.document.DLPULGA3form.Find.disabled = true;
                }
	else
		{
		return false;
		}
}


<select name="statusValue" onChange="checkIfAll()">
			<% if (status.equals("Accepted")) {%>
			      <option selected>Accepted</option>
			      <option value ="Rejected">Rejected</option>
			      <option value ="Proposed"> Proposed</option>
			      <option value ="All">All</option>
			<% } else  if (status.equals("Rejected")) {%>
			      <option value ="Accepted">Accepted</option>
			      <option selected>Rejected</option>
			      <option value ="Proposed">Proposed</option>
			      <option value ="All">All</option>
			<% } else if (status.equals("Proposed")) {%>
			      <option value ="Accepted">Accepted</option>
			      <option value ="Rejected">Rejected</option>
			      <option selected>Proposed</option>
			      <option value ="All">All</option>
			<% } else { %>	
			      <option value ="Accepted">Accepted</option>
			      <option value ="Rejected">Rejected</option>
			      <option value ="Proposed">Proposed</option>
		              <option selected>All</option>					
			<% }      %>
		</select>

I can’t quite understand what is happening here.

I can’t see where selectedIndex is defined - but then I don’t think you have posted everything?

Maybe you can post a link to the site? Or you can view the source on the site and post that.

ok, where should the selectedIndex be defined?

Well if I understand what you’re trying to do then I think you need to pass it into the function.

I’d pass the this pointer into the function.

i.e.

onChange=“checkIfAll(this)”

then in your function you can access

this.selectedIndex

Alternatively you could pass this.selectedIndex in directly if that is the only attribute you want.

PS - Obviously you could access that attribute directly from the DOM - but its not very modula approach, and IMHO not good coding practise.

Dale what you say is correct however in the example code given cjdesigns is accessing the form element using the

document.formname.formelement

method.

cjdesigns pass the this pointer to the function then use it as a reference to the form.


<% String status = DLPULGA3Bean.getStatus(); %>

function checkIfAll(th)
{

	if (th.options[th.selectedIndex].value == "All")
		{
	th.form.Find.disabled = true;
                }
	else
		{
		return false;
		}
}


<select name="statusValue" onChange="checkIfAll(this)">
			<% if (status.equals("Accepted")) {%>
			      <option selected>Accepted</option>
			      <option value ="Rejected">Rejected</option>
			      <option value ="Proposed"> Proposed</option>
			      <option value ="All">All</option>
			<% } else  if (status.equals("Rejected")) {%>
			      <option value ="Accepted">Accepted</option>
			      <option selected>Rejected</option>
			      <option value ="Proposed">Proposed</option>
			      <option value ="All">All</option>
			<% } else if (status.equals("Proposed")) {%>
			      <option value ="Accepted">Accepted</option>
			      <option value ="Rejected">Rejected</option>
			      <option selected>Proposed</option>
			      <option value ="All">All</option>
			<% } else { %>	
			      <option value ="Accepted">Accepted</option>
			      <option value ="Rejected">Rejected</option>
			      <option value ="Proposed">Proposed</option>
		              <option selected>All</option>					
			<% }      %>
		</select>

vibes :slight_smile:

Wasn’t trying to fix everything, only the stated problem :smiley:

Originally posted by dale_burrell
Wasn’t trying to fix everything, only the stated problem :smiley:

true :slight_smile: sorry misread your post.

works like a dream now-thank you both!!!