SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Location
    UK, London
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry [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?


    Code:
    <% 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>
    If you can talk, you can sing, if you can walk, you can dance!!

  2. #2
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Location
    UK, London
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, where should the selectedIndex be defined?
    If you can talk, you can sing, if you can walk, you can dance!!

  4. #4
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Last edited by dale_burrell; Nov 28, 2002 at 06:10.
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  5. #5
    SitePoint Member
    Join Date
    Nov 2002
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

    Code:
    <% 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
    Last edited by vibes35; Nov 28, 2002 at 07:13.

  6. #6
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wasn't trying to fix everything, only the stated problem
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  7. #7
    SitePoint Member
    Join Date
    Nov 2002
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by dale_burrell
    Wasn't trying to fix everything, only the stated problem
    true sorry misread your post.

  8. #8
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Location
    UK, London
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking

    works like a dream now-thank you both!!!!
    If you can talk, you can sing, if you can walk, you can dance!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •