-6

How can I check if the radio button is checked or not by calling inline function of javascript?

<html>
<head>
<title>Test this</title>
<script>
var cnfrm= function(){
var x=document.getElementById('rdio2').value;
if (x==null || x=="")
    {
       confirm('are you sure?');
       // return false;
      // redirect to where you want
    }

   }
</script>
</head>
<body>
<form method=post action=index.html>
<br><input type=radio name=rdio id=rdio2 value=b>
<br><input type=radio name=rdio id=rdio2 value=c>
<br>
<input type=submit value=Submit onclick=cnfrm()>
</form>
</body>
</html>

Right now I am not getting anything and it ignore cnfrm function, So I want it to execute so that I can check weather it is selected or not.

Arslan Arshad
  • 31
  • 1
  • 4

1 Answers1

1

Simply use .checked instead of .value.

  if(document.getElementById('rdio2').checked==true) {
    return true;
  } else if(document.getElementById('rdio3').checked==true) {
    return true;
  } else if(document.getElementById('rdio4').checked==true) {
    return true;
  }  else {
    return confirm('are you sure?');
  }

FIDDLE DEMO

Wit Wikky
  • 1,542
  • 1
  • 14
  • 28