Reading value of a radio button using javascript

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.

dsr79Asked:
Who is Participating?
 
Russell2566Connect With a Mentor Commented:
This should work for you. I wrote the whole thing out as there were several issues. One of them being an "=" sign in your if statment instead of a "=="

<script>
      function validate(form) {
            var n;
            var self_mailer = null;

            for (n=0; n < form.MailingSelf.length; n++)
                  if (form.MailingSelf[n].checked)
                        self_mailer = form.MailingSelf[n].value;

            if (!self_mailer) {
                  alert("Please Select If You Want To Be Included In The Mailing");
                  form.submit();
            }

            if (self_mailer == "Yes" && form.mailingselfaddress.value.length == 0) {
                  alert("Please enter the address that you want to include in the mailing");
                  form.mailingselfaddress.focus();
                  return;            
            }
      }
</script>

<form name="myForm">
      <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>
      <input type="button" onClick="validate(this.form);" value="submit">
</form>
0
 
dsr79Author Commented:
Hi Russell,

with one minor change (removing form.submit) it works great...Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.