0

I'm trying to use Javascript to pass a value from a radio button to the next HTML page. However the Javascript I came up with seems to not able to access the radio buttons. Why?

HTML code:

<body>
<div id="frontContent">

        <p id ="levelContent">Levels: </p>
        <form id="levelSelection">
            <input type="radio" name="level" value="1" >Level 1</input>
            <input type="radio" name="level" value="2">Level 2</input>
        </form>

            <p id="startButton"><a href="index.html">Start</a></p>

        <p id ="scoreBoard">Highest Score: <span id="score">0</span></p>

</div>
</body>

Javascript code:

var radios = document.getElementsByName('level');
        alert(radios.length);
for (var i = 0, length = radios.length; i < length; i++) {
    if (radios[i].checked) {
        alert(radios[i].value);
        break;
    }
}
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
ddd suman
  • 55
  • 1
  • 8

6 Answers6

3

This code might help:

    <!DOCTYPE html>
<html>
<body>
<div id="frontContent">

        <p id ="levelContent">Levels: </p>
        <form id="levelSelection">
            <input type="radio" name="level" value="1" >Level 1</input>
            <input type="radio" name="level" value="2">Level 2</input>
        </form>

            <p id="startButton"><a href="index.html">Start</a></p>
<input type="button" onclick="clickButton()" value="clickMe"/> 
        <p id ="scoreBoard">Highest Score: <span id="score">0</span></p>

</div>
<script>
var radios = document.getElementsByName('level');
        alert(radios.length);
function clickButton(){
var radioVal = 0;
for (var i = 0, length = radios.length; i < length; i++) {
    if (radios[i].checked) {
        alert(radios[i].value);
        radioVal = radios[i].value;
        break;
    }
}
window.location.href= "index.html?radioValue="+radioVal;
}
</script>
</body>
</html>
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
Sudhansu Choudhary
  • 3,322
  • 3
  • 19
  • 30
  • must say I'm navigating on click of a button, but same could be mocked for a click on a link. On index.html page you can retrieve the value of radio button from URL params. – Sudhansu Choudhary Jun 16 '15 at 16:37
0
<div id="frontContent">

        <p id ="levelContent">Levels: </p>
        <form id="levelSelection">
            <input type="radio" onclick="checkRadio()" name="level" value="1"  >Level 1</input>
            <input type="radio" onclick="checkRadio()" name="level" value="2" checked='true'>Level 2</input>
        </form>

            <p id="startButton"><a href="index.html">Start</a></p>

        <p id ="scoreBoard">Highest Score: <span id="score">0</span></p>

</div>

<script>
     function checkRadio(){
        var radios = document.getElementsByName('level');
        alert(radios.length);
        for (var i = 0, length = radios.length; i < length; i++) {
          if (radios[i].checked) {
           alert(radios[i].value);
           callRedirect(radios[i].value);
           break;
         }
      }

    }

function callRedirect(selectedVal){
  window.location.href = "urlHTMLPageURL?radioOption="+selectedVal;
}    
</script> 
Swaprks
  • 1,553
  • 11
  • 16
0

If you are sending to another page, you must set the <form> tag with action and method, for example: method="get" action="index.html", and use a submit button instead of the link.

So, in the other page, you can get the value from query string.

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
Ricardo Pontual
  • 3,749
  • 3
  • 28
  • 43
-1

Be sure to put your JavaScript at the end of the file, like this:

<body>
  <div id="frontContent">

        <p id ="levelContent">Levels: </p>
        <form id="levelSelection">
            <input type="radio" name="level" value="1" >Level 1</input>
            <input type="radio" name="level" value="2">Level 2</input>
        </form>

            <p id="startButton"><a href="index.html">Start</a></p>

        <p id ="scoreBoard">Highest Score: <span id="score">0</span></p>

  </div>

  <script>

    var radios = document.getElementsByName('level');
    alert(radios.length);
    for (var i = 0, length = radios.length; i < length; i++) {
      if (radios[i].checked) {
        alert(radios[i].value);
        break;
      }
    }
  </script>
</body>
Danny Delott
  • 6,756
  • 3
  • 33
  • 57
-1

Initially none of the radio option is selected. So the code doesn't enter the if block

http://jsfiddle.net/sez8c3xL/ check the fiddle for an example

<div id="frontContent">

        <p id ="levelContent">Levels: </p>
        <form id="levelSelection">
            <input type="radio" name="level" value="1"  >Level 1</input>
            <input type="radio" name="level" value="2" checked='true'>Level 2</input>
        </form>

            <p id="startButton"><a href="index.html">Start</a></p>

        <p id ="scoreBoard">Highest Score: <span id="score">0</span></p>

</div>

<script>

    var radios = document.getElementsByName('level');
        alert(radios.length);
for (var i = 0, length = radios.length; i < length; i++) {
    if (radios[i].checked) {
        alert(radios[i].value);
        break;
    }
}

</script>  
Himanshu Tanwar
  • 906
  • 6
  • 18
-1

Hello here is the working script to get the radio buttons' values:

<script>
    var elem = document.getElementById('levelSelection').elements;
    for(var i = 0; i < elem.length; i++)
    {
        if(elem[i].type =="radio" && elem[i].checked == true)
        {
           alert(elem[i].value);
        }
    }
  </script>
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
Luca
  • 108
  • 1
  • 11