Table with Radio input to pick the selected value

Asked

Viewed 1,943 times

0

Hello, I have a table that has a line where you can select a radio button. When selecting the save button I want to get the values that were selected. I can pick up the radio button text, however I’m not getting the selected value.

enter the code

    var table = document.getElementById("tbl");
        for (var i = 1,row; row = table.rows[i]; i++) {
            rows = table.getElementsByTagName('tr');
            var cells = rows[i].getElementsByTagName('td');

          
            alert(cells[2].innerText);
        }
         <tr>
             
                <td>
                  
                <label>
                    Presente
                    <input type="radio" name="@item.Id" value="1" checked>
                </label>

                <label>
                    Ausente
                    <input type="radio" name="@item.Id" value="0">
                </label>
                <td/>

            </tr>

1 answer

0


Use the mercy Document.querySelectorAll to search for the desired element. It will return an array, as it is an input of type radio, you will only take the element [0].

input[type=radio] - finds all radio type input elements

:checked - all radio type input that are selected

document.querySelectorAll('input[type=radio]:checked')[0].value;
  • Document.querySelectorAll('input[type=radio]:checked') returns an array with all selected inputs, iterate using for, while...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.