We help IT Professionals succeed at work.

Reading value of a radio button using javascript

dsr79
dsr79 asked
on
1,363 Views
Last Modified: 2008-01-09
Hi,

I have a HTML page with a form that has a bunch of controls.  Of these there are three particular controls that I am having problems with.  There is a radio button group (two radio buttons) and one text box.  The radio button group consists of two buttons that can take "Yes" or "No" values.  The user has to select one of the radio buttons and if the user selects the one that has the "Yes" value, he has to be fill in a value in the text box.  If the user selects "No", the text boxes are not mandatory.

Here is the HTML code


                        <dt><label class="mandatory">Do you want to be included in the mailing?</label></dt>
                        <dd><input class="radio" name="MailingSelf" type="radio" value="Yes" /><span>Yes. &nbsp;&nbsp;<font color="#0000FF" size="-5">If yes, please enter mailing address below</font></span> </dd>
                        <dd><input class="radio" name="MailingSelf" type="radio" value="No" />No. </dd>
                        <dt><label >Mailing Address : </label>
                        </dt>                        
                        <dd><textarea name="mailingselfaddress" cols="" rows=""></textarea></dd>

Here is the javascript that I am using when the form is submitted.

var self_mailer=false;
var self_mailer_value="";
for (counter = 0; counter < form.MailingSelf.length; counter++)
      {
      // If a radio button has been selected it will return true
      // (If not it will return false)
      if (form.MailingSelf[counter].checked)
            self_mailer = true;
            self_mailer_value=form.MailingSelf[counter].value;
      }
      if (!self_mailer)

      {
            alert("Please Select If You Want To Be Included In The Mailing");
            form.MailingSelf.focus();
            return;
      }
      
      if (self_mailer_value=="Yes" && form.mailingselfaddress.value="")
      {
            alert("Please enter the address that you want to include in the mailing");
            form.mailingselfaddress.focus();
            return;            
      }
      

This is not working.  It will prompt be to select a radio button if it is not selected but once I select "Yes", it is not forcing me to fill in the text box.  Why is that?  Is there anything I can do to fix it.

Comment
Watch Question

Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi Russell,

with one minor change (removing form.submit) it works great...Thanks.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.