0

Please see the fiddle link at the bottom. I have two questions:

  1. How to add HTML text to these radio buttons. I have to give them $ and % value (for the user).
  2. Find the values of every radio button selected. For example, the user added 10 rows (each having 2 radio buttons). I have iterated a loop to find the input type and see if the button is checked and then find its value.

NOT WORKING, guide me what wrong am I doing.

FIDDLE

var i=0;
window.myAdd = function(){
  var x = i;
  var butts = document.createElement("INPUT");
  butts.setAttribute("type", "radio");
  butts.setAttribute("name", "currency"+x);
  butts.setAttribute("value", "%");
  butts.setAttribute("id", "radio"+i);
  //var node = document.createTextNode("%");
  //butts.appendChild(node);
  i=i+1;
  //console.log(butts);

  var butts_1 = document.createElement("INPUT");
  butts_1.setAttribute("type", "radio");
  butts_1.setAttribute("name", "currency"+x);
  butts_1.setAttribute("value", "$");
  butts_1.setAttribute("id", "radio"+i);
  i=i+1;
  //console.log(butts_1);

  var row = document.createElement("TR");
  //document.getElementById('tab').appendChild(butts);
  //document.getElementById('tab').appendChild(butts_1);
  row.appendChild(butts);
  row.appendChild(butts_1);
  document.getElementById('tab').appendChild(row);
  x=x+1;
}
window.myfunction = function(table){
 //var x = String(document.getElementById('radioP').value);
 //alert(x);
 for(var i=0;i<table.elements.length;i++){
    if(table.elements[i].type =='radio'){
        if(table.elements[i].checked == true){
            alert(table.elements[i].value);
        }
    }
 }
}
2
  • Are you just trying to label the inputs and check their values? Commented Dec 3, 2014 at 18:12
  • No, I want to label them then the user can select as many checkboxes then by clicking on GET VALUE it must show all checked values. Commented Dec 3, 2014 at 18:13

2 Answers 2

1

I have corrected your script to work:

var i = 0;
window.myAdd = function() {
  var x = i;
  var butts = document.createElement("INPUT");
  butts.setAttribute("type", "radio");
  butts.setAttribute("name", "currency" + x);
  butts.setAttribute("value", "%");
  butts.setAttribute("id", "radio" + i);
  //var node = document.createTextNode("%");
  //butts.appendChild(node);
  i = i + 1;
  //console.log(butts);

  var butts_1 = document.createElement("INPUT");
  butts_1.setAttribute("type", "radio");
  butts_1.setAttribute("name", "currency" + x);
  butts_1.setAttribute("value", "$");
  butts_1.setAttribute("id", "radio" + i);
  i = i + 1;
  //console.log(butts_1);

  var row = document.createElement("TR");
  //document.getElementById('tab').appendChild(butts);
  //document.getElementById('tab').appendChild(butts_1);
  row.appendChild(butts);
  row.appendChild(butts_1);
  document.getElementById('mytable').appendChild(row);
  x = x + 1;
}
window.myfunction = function(table) {
  //var x = String(document.getElementById('radioP').value);
  //alert(x);
  //debugger;
  for (var i = 0; i < table.rows.length; i++) {
    var row = table.rows[i];
    for (var j = 0; j < row.childNodes.length; j++) {
      if (row.childNodes[j].type == 'radio') {
        if (row.childNodes[j].checked == true) {
          alert(row.childNodes[j].value);
        }
      }
    }

  }
}
<button onclick='myAdd()'>Add Radio Buttons</button>
<table id="mytable" name="mytable"></table>
<button onclick='myfunction(document.getElementById("mytable"))'>GET VALUE</button>

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for putting light on the table.rows.length and row.childNodes.length....but it is not working..Uncaught TypeError: Cannot read property 'rows' of undefined
I've changed the selectors in the markup, try to run my snippet, just click on button "Run code snippet"
0

1) You have to use <label> element, e.g.:

<input id="radio_1" type="radio" value="1" />
<label for="radio_1">$</label>

<input id="radio_2" type="radio" value="2" />
<label for="radio_2">%</label>

2) You have to iterate through them. Considering all of the radios are within some div with class "container", you'll need something like this:

document.getElementById('get_values').addEventListener('click', function() {
    var radios = document.querySelectorAll('.container input[type="radio"]');
    values = {};
    for(i=0; i<radios.length; i++) {
        var radio = radios[i];
        var name = radio.getAttribute('name');
        if(radio.checked) {
            values[name] = radio.value;
        } else {
            values[name] = values[name] || null;
        }
    }
    alert(JSON.stringify(values));
});

See this fiddle: http://jsfiddle.net/andunai/gx6quyo0/

1 Comment

Hi, totally ignored the querySelectorAll() thing..Thanks for that!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.