views:

56

answers:

2

I am using below code to delete HTML Table row using javascript but its giving me error.

using below code i am creating a column at run time using javascript which contains delete Anchor tag.

            var tbody = document.getElementById("<%= tblcriteria.ClientID %>").getElementsByTagName("TBODY")[0];

var row = document.createElement("TR")
 var td4 = document.createElement("TD");
 var code = "<a href=\"javascript:deleteCriteria(this.parentNode.parentNode.rowIndex);\">delete</a>";
    td4.setAttribute("align", "center");
    td4.innerHTML = code;
   row.appendChild(td4);
  tbody.appendChild(row);

Below function i am using to delete current row of html table:

function deleteCriteria(i) {
        if (window.confirm('Are you sure you want to delete this record?') == true) {
            document.getElementById("<%= tblcriteria.ClientID %>").deleteRow(i);

        }
    }

its giving me below error:

'this.parentNode.parentNode.rowIndex' is null or not an object

Please tell me where i am making mistake...

+3  A: 

this does not point to the <a> element, but to the window.

Sjoerd
then what should i do? i am to show image for 'Delete'
Rajesh Rolen- DotNet Developer
Use onclick instead. You could still use an <a> element, or you could use an image.
Sjoerd
+2  A: 

As @Sjoerd has mentioned you should use onClick instead.

This line

var code = "<a href=\"javascript:deleteCriteria(this.parentNode.parentNode.rowIndex);\">delete</a>";

should read

var code = "<a href=\'#\' onclick=\'javascript:deleteCriteria(this.parentNode.parentNode.rowIndex);\'>delete</a>";

Barry
No point in the `javascript:` label if you're using `onclick`.
J-P