1

I'm new to javascript , I've lot of checkboxes and recently I decided to put those inside a table which will look it more prettier ,( one question per check box inside a table cell ).

I'm working on a code which can check if on a specific cell , particular question / checkbox is check or not. And do something if that is checked .

Here is the sample HTML code :-

<tr id ="row2">
    <td><div>Disable Auto Save  : <input type="checkbox" name="disautosave" value="disableauto" id = "disableauto"></div></td>
    <td><div>Enable Stack Monitoring  : <input type="checkbox" name="stackmon" value="enablestackmon" id = "enablestackmon"></div></td>
    <td>3</td>

And here is the Javascript code , if I use the alert here , it just pop up and alert with "Disable Auto Save :" which is the content of the cell

var Row = document.getElementById("row2");
var Cells = Row.getElementsByTagName("td");
alert(Cells[0].innerText);

However , I was looking for the checkbox value , i.e. to check if checkbox on that given cell is checked or not. I tried the below code ...but its not working.

if (Cells[0].checked){
    alert("Checked");
    //alert(Cells[1].innerText);
}
2
  • You should put that in a snippet or fiddle. Commented Dec 27, 2015 at 19:30
  • Got the answer while playing .....Here if (document.getElementById("row2").getElementsByTagName("td")[0].getElementsByTagName('input')[0].checked==true) { alert ("Hola"); } Commented Dec 27, 2015 at 20:11

3 Answers 3

3

Cells in your code refers to a bunch of td tags , there's no checked property on them

use var checkboxes = document.querySelectorAll("row2 input[type=checkbox]") to get all your checkboxes and then use checked on them individually, like : checkboxes[0].checked and so on

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

1 Comment

In the cells , am using td tags to put the table data for that row. If you look it got checkbox with id , name & value. Not sure if that makes sense , but if I dont put that checkbox inside the table. I can easy access if that is checked using below code , which is working : var vDisauto = (document.getElementById("disableauto").checked); Later I check this with a if sttement to match if this is true ,,,do this etc logic
0

Answer taken from the question:

Got the answer while trying and playing with it

if (document.getElementById("row2").getElementsByTagName("td")[0].getElementsByTagName('input')[0].checked==true)
 {

 alert ("Hola");

 }

Comments

-1
var chk = tableRows[x].cells[5].firstChild; 
// converts from table cell to <input> i get inside of <td> an <input>

Comments

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.