SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Testing for a radio value using the .value method?

    I am trying to test for the selected value of a set of radio buttons. What I am doing is testing all required form fields for values, and doing this by using a switch() on the type of field.

    If the field type is a radio, then I use the getElementsByName() method on that group of radio buttons, then iterate through those testing to see of one is selected. If one is selected then I want to know the value, but when I get to that part of the code, I get the error, "radios[j] has no properties". Am I trying to use the .value property incorrectly?

    Is there a better way of testing for the value?

    Thanks for any help!

    Code:
              for(var i = 0; i < requiredFields.length; i++)
              {
                switch(document.getElementById(requiredFields[i]).type)
                {
                  case "text":
                    if(document.getElementById(requiredFields[i]).value != "")
                      reveal = true;
                    else
                      reveal = false;
                    break;
                    
                  case "select-one":
                    if(document.getElementById(requiredFields[i]).value != "")
                      reveal = true;
                    else
                      reveal = false;
                    break;
                    
                  case "radio":
                    var radios = document.getElementsByName(requiredFields[i]);
                    for(var j = 0; j < radios.length; j++)
                    {
                      if(radios[j].checked == true)
                      {
                        reveal = true;
                        j = radios.length;
                      }
                      else
                        reveal = false;
                        
                      //  show/hide additional fields
    
    // This is where I test for the value of the radio button
                      switch(radios[j].value)
                      {
                        case "zipper":
                          document.getElementById("zipperSide").disabled = "";
                          break;
                        case "No":
                          document.getElementById("zipperSide").disabled = "disabled";
                          break;
                        default:
                          break;
                      }
                    }
                    break;
                } // End switch
                
                if(reveal == false)
                  break;
              } // End loop
    John

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,788
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You have set j to one past the end of the array from within the prior loop in order to exit the loop.

    Instead of setting reveal to true or false set it to j or -1 and then test radios[reveal].value instead of radios[j] value.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent! Thanks. Sometimes I can't see the forest for the trees.
    John

  4. #4
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alternatively, you could just replace j = radios.length; with break;.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.


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
  •