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...

1
  • 2
    You might want to look into jQuery - jquery.com - for much easier DOM manipulation and cross browser support. Commented Jun 21, 2010 at 6:58

2 Answers 2

3

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

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

1 Comment

Use onclick instead. You could still use an <a> element, or you could use an image.
2

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>";

1 Comment

No point in the javascript: label if you're using onclick.

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.