admin管理员组

文章数量:1391735

I have this javascript

function deleteRow(tableID) 
      {
            try {
            var table = document.getElementById(collct_table_body);
            var rowCount = table.rows.length;

            for(var i=0; i<rowCount; i++) {
                var row = table.rows[i];
                var chkbox = row.cells[0].childNodes[0];
                if(null != chkbox&& true == chkbox.checked) {
                    if(rowCount <= 1) {
                        alert("Cannot delete all the rows.");
                        break;
                    }
                    table.deleteRow(i);
                    rowCount--;
                    i--;
                }


            }
            }catch(e) {
                alert(e);
            }

But when I try to delete a row using this it shows an error "cannot read property 'rows' of null type error"

Is there any error in this javascript Im using?Can some one help me out?

I have this javascript

function deleteRow(tableID) 
      {
            try {
            var table = document.getElementById(collct_table_body);
            var rowCount = table.rows.length;

            for(var i=0; i<rowCount; i++) {
                var row = table.rows[i];
                var chkbox = row.cells[0].childNodes[0];
                if(null != chkbox&& true == chkbox.checked) {
                    if(rowCount <= 1) {
                        alert("Cannot delete all the rows.");
                        break;
                    }
                    table.deleteRow(i);
                    rowCount--;
                    i--;
                }


            }
            }catch(e) {
                alert(e);
            }

But when I try to delete a row using this it shows an error "cannot read property 'rows' of null type error"

Is there any error in this javascript Im using?Can some one help me out?

Share Improve this question edited Dec 12, 2013 at 17:22 Tadmas 6,3784 gold badges40 silver badges31 bronze badges asked Dec 12, 2013 at 17:14 user3079558user3079558 2
  • Instead of editing the question to show [solved], you should accept the answer below to indicate that solved your problem. – Tadmas Commented Dec 12, 2013 at 17:23
  • Sorry, I was trying to be more constructive instead of just saying "I rolled back your edit because what you did is against our guidelines." Or just rolling back with no explanation. I didn't intend it to sound as harsh as I can see now it is - my apologies. See also meta.stackexchange./questions/116101/… – Tadmas Commented Dec 12, 2013 at 19:29
Add a ment  | 

1 Answer 1

Reset to default 3

You're missing the quotes

document.getElementById("collct_table_body");

or, may be this is a variable (which I don't see in your code, global var?? we will never know)

var collct_table_body = "someID";

本文标签: Type error cannot read property 39rows39 of null when deleting a row using javascriptStack Overflow