Delete HTML Table Row using Javascript
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...
this
does not point to the <a>
element, but to the window.
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>";
精彩评论