0

undefined output!! am i missing sth ? any idea ?

here is the radio button inputs :

  <tr> 
   <td>Yes <input type="radio" name="email"  id="email" value="yes"/></td> 
   <td></td> 
   <td>No <input type="radio" name="email" id="email" value="no"checked="checked"/></td> 
  </tr>



 <input type="button" value="submit" onclick="doIt()" style="width: 150px; height:30px" />


    <script type="text/javascript" language="javascript">

       function doIt() {

          var emailopt = document.getElementById('email').checked.value;

        alert (emailopt);


    }

Praveen
  • 55,303
  • 33
  • 133
  • 164
user3516668
  • 109
  • 5
  • `var emailopt = document.getElementById('email').checked;` And you're not closing the ` – briosheje Apr 28 '14 at 13:30
  • 1
    The "id" should be unique – Oyeme Apr 28 '14 at 13:30
  • Just comment the js function statement and put alert inside that, it is not working for that as well. – Rubyist Apr 28 '14 at 13:33

2 Answers2

0

The way you have tried is completely wrong. Instead use the below approach

1) Iterate through all the radio buttons with name property
2) Now using checked attribute, place an if statement and then extract the value

  function doIt() {
      var emailopt = document.getElementsByName('email');
      for (i = 0; i < emailopt.length; i++) {
          if (emailopt[i].checked) {
              alert(emailopt[i].value)
          }
      }
  }

Fiddle

Praveen
  • 55,303
  • 33
  • 133
  • 164
0

Try using this:

function doIt() {

          var emailopt = document.getElementById('email').checked;

        alert (emailopt);


    }

It will return true and false. Lets me know if you have any questions.I hope it will help you.

Sonu Sindhu
  • 1,752
  • 15
  • 25