SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Attempting to get all select option values, and check if one or more is set to 'No'

    I am attempting to get all select option values in a form, and check if one or more is set to 'No'. If so, then I want to display something with an ID, else have that ID set to display:none. Here's what I have so far. Using the Firebug extension, I am getting this error: "document.forms[0].elements[i].options has no properties" - so I know I'm not effectively grabbing the values from the form. If someone would take a look at it, I'd appreciate it...

    Code:
    function getSelects()
    {
    	var answer = null;
    	for (var i = 0; i < document.forms[0].elements.length; i++)
    	{
    		if (document.forms[0].elements[i].selectedIndex != 0)
    		{
    			answer = document.forms[0].elements[i].options[document.forms[0].elements[i].selectedIndex].value;
    		}
    	}
    
    	if (answer == 'No')
    	{
    		document.getElementById('issueTable').style.display = 'block';
    	}
    
    	if (answer != 'No')
    	{
    		document.getElementById('issueTable').style.display = 'none';
    	}
    }

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You're checking all fields, but only select field can have property 'options'. Use document.formName.selectFieldName to specify the select field.
    Saul

  3. #3
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doing it that way, I would need to check each and every select individually, right? I am looking for a way to go through and look at all the values. Maybe I am misunderstanding what you're saying though.

  4. #4
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ah, you have many select fields. Then try adding a condition to check if it is a select field:
    Code:
    if(document.forms[0].elements[i].options){
      if (document.forms[0].elements[i].selectedIndex != 0)
      {
    	answer = document.forms[0].elements[i].options[document.forms[0].elements[i].selectedIndex].value;
      }
    }
    Saul

  5. #5
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much. It's all good now. I owe you a drink or something.

  6. #6
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You're welcome. A drink would be nice.
    Saul

  7. #7
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, it works, but if Yes is selected, or some non-No answer, it will re-hide the table (even if some of the previously selected ones are still No).

  8. #8
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code:
    if (document.forms[0].elements[i].selectedIndex != 0)
     {
      answer = document.forms[0].elements[i].options[document.forms[0].elements[i].selectedIndex].value;		
      break;
    }
    Saul

  9. #9
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Interesting, that reversed the behavior. Now selections of non-No before the No cause the table to re-hide.

  10. #10
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    With the help of another friend, I got it working. Here's the full code...

    Code:
    function getSelects()
    {
    	var x = document.forms[0];
    	document.getElementById('issueTable').style.display = 'none';
    	for (var i = 0; i < x.elements.length; i++)
    	{
    		if
    		(
    			x.elements[i].options &&
    			x.elements[i].selectedIndex != 0
    		)
    		{
    			answer = x.elements[i].options[x.elements[i].selectedIndex].value;
    			if (answer == 'No')
    			{
    				document.getElementById('issueTable').style.display = 'block';
    			}
    		}
    	}
    }


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
  •