Why do you give me the error [object NodeList]?

2

I'm trying to just select a radio button to be saved in a variable in php, but for now I just managed to do it in javascript and it gives me the error [object NodeList].

function updateTotal() {
    var radios = document.getElementsByName('shipping');

    document.getElementById('prueba').innerHTML = radios;

}
<strong>estado</strong> = frio
<label>
    <input name="shipping" type="radio" id="RadioGroup1_0" value="calor" checked="checked" onclick="updateTotal()" />
</label>calor
 <input type="radio" name="shipping" value="frio" id="RadioGroup1_0" onclick="updateTotal();" />
 <br />
 <span id="prueba">aaaa</span>
    
asked by Kaiserdj 26.07.2017 в 22:39
source

1 answer

2

getElementsByName(name) returns an array of elements (NodeList) with the indicated name (which in your case would be 2).

I guess what you want is to print the value of radio selected and for that you should look for the element that has the property checked equal to true and then get the property value of it:

function updateTotal() {
    var radios = document.getElementsByName('shipping');

     for(var i = 0;i < radios.length;i++)
     {
        if(radios[i].checked)
        {
         document.getElementById('prueba').innerHTML = radios[i].value;
        }
     }

   

}
<strong>estado</strong> = frio
<label>
    <input name="shipping" type="radio" id="RadioGroup1_0" value="calor" checked="checked" onclick="updateTotal()" />
</label>calor
 <input type="radio" name="shipping" value="frio" id="RadioGroup1_0" onclick="updateTotal();" />
 <br />
 <span id="prueba">aaaa</span>
    
answered by 26.07.2017 / 22:50
source