SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    grabbing the value of a radio

    I know this is pretty basic, but this won't work no matter what.

    I have 2 radio buttons, both named johnCushingApp.

    Here's the code:
    Code:
    function validForm(passForm)
    {
     if(passForm.appl_ssg_cde.value == "blank")
     {
      alert("You must select an SSG")
      return false
     }
     if(passForm.johnCushingApp.value == "yes")
     {
      alert("Yes")
      if(passForm.RPTG_APPL_ID.value == "blank")
      {
       alert("You must select a SSG")
       return false
      }
     }
     return true
    }
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  2. #2
    SitePoint Evangelist Mr. Brownstone's Avatar
    Join Date
    May 2002
    Location
    Relative
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If two radio-buttons is all you have, you can solve the problem without JavaScript. Just include the attribute checked="checked" in the radio-button that you want to use as the default:
    HTML Code:
    <input type="radio" name="johnCushingApp" value="1" checked="checked" />
    <input type="radio" name="johnCushingApp" value="2" />
    Being radio-buttons, the browser will make it impossible to de-select both of them.
    Of course, that's just my opinion. I could be wrong.

  3. #3
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So how does that make it so i can check against it to know whether or not the "yes" radio is checked.
    And here's the html:
    HTML Code:
    <input type="radio" name="johnCushingApp" value="no" checked>
    <input type="radio" name="johnCushingApp" value="yes">
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  4. #4
    SitePoint Evangelist Mr. Brownstone's Avatar
    Join Date
    May 2002
    Location
    Relative
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can try this:
    HTML Code:
    <input type="radio" name="johnCushingApp" value="no" checked="checked" />
    <input type="radio" [u]id="JCAYes"[/u] name="johnCushingApp" value="yes" />
    Code:
    function validForm(passForm)
    {
    
    var bJCAYesChecked = document.getElementById ( 'JCAYes' ).checked;
    
     if(passForm.appl_ssg_cde.value == "blank")
     {
      alert("You must select an SSG")
      return false
     }
     if( bJCAYesChecked )
     {
      alert("Yes")
      if(passForm.RPTG_APPL_ID.value == "blank")
      {
       alert("You must select a SSG")
       return false
      }
     }
     return true
    }
    Of course, that's just my opinion. I could be wrong.

  5. #5
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That works, thx!
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  6. #6
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you're doing alot of this sort of thing, I'd like to suggest abstracting the process -- I do this with my fValidate script and it works great - just pass it a reference to the element.
    Code:
    functon getElementValue( elem )
    {
    	switch ( elem.type )
    	{
    		case 'text' :
    		case 'password' :
    		case 'textarea' :
    		case 'hidden' :
    		case 'file' :
    			return elem.value;
    		case 'radio':
    		case 'select-single':
    			if ( typeof elem.length == 'undefined' )
    			{
    				return elem.value;
    			} else {
    				for ( var i = 0; i < elem.length; i++ )
    				{
    					choice = ( elem.type == 'radio' ) ? "checked" : "selected";
    					if ( elem[i][choice] )
    					{
    						return elem[i].value;
    					}
    				}
    			}
    		case 'select-multiple' :
    		case 'checkbox' :
    			if ( typeof elem.length == 'undefined' )
    			{
    				return elem.value
    			} else {
    				var returnValues = new Array();
    				for ( var i = 0; i < elem.length; i++ )
    				{
    					choice = ( elem.type == 'checkbox' ) ? "checked" : "selected";
    					if ( elem[i][choice] )
    					{
    						returnValues.push( elem[i].value );
    					}
    				}
    				return returnValues;
    			}
    		default: return null;
    	}
    }
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





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
  •