0

I am trying on following code;

Why doesn't deleteRow() alert "hi" when we click on delete button for first time (nor it delete row)? Surprisingly it will work perfectly second time.

HTML

<div style="height: 190px;overflow: auto;left:220px;width:710px;" id="filterTable">
            <table id="filterTableBody" style="border-collapse: collapse; border: 1px solid black;width:690px;" border="1">
                <tbody><tr bgcolor="#FF6600">
            <td><strong>
                    and/or&nbsp;&nbsp;&nbsp;
                </strong></td>
                <td><strong>
                    Column Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                </strong></td>
                <td><strong>
                    operator&nbsp;&nbsp;&nbsp;
                </strong></td>
                <td><strong>
                    Filter&nbsp;&nbsp;&nbsp;
                </strong></td>

                <td><strong>
                    Delete&nbsp;&nbsp;&nbsp;
                </strong></td>
            </tr>
            <tr><td>&nbsp;</td><td>WORKGROUP_NAME</td><td>!=</td><td>ABDEL HAMEID</td><td><a href="javascript:deleteRow()"><img src="/images/delete.gif"></a></td></tr></tbody></table>
            </div>

Javascript

 function deleteRow(){
        var table = document.getElementById('filterTableBody');
        var rows1 = table.getElementsByTagName('tbody')[0].getElementsByTagName('tr');
        for (var i = 0; i < rows1.length; i++) {
            rows1[i].onclick = (function() {
            alert("hi");
                table.deleteRow(this.rowIndex);
                var oTable = document.getElementById('filterTableBody');

            //gets rows of table
            var rowLength = oTable.rows.length;
            for (i = 1; i < rowLength; i++){

                   var oCells = oTable.rows.item(i).cells;
                   //gets cells of current row
                   var cellLength = oCells.length-1;
                       for(var j = 0; j < cellLength; j++){
                           oCells.item(j).innerHTML = "";
                           break;
                       }
                       break;
            }
        });
    }

} 

Why doesn't the code run in first click and why it runs in the second?

2 Answers 2

1

The reason is because the onclick event handler for the rows are getting attached only when the Delete button is clicked for the first time.

They have to be attached onload itself. You can do it like below:

window.onload = deleteRow;

Demo Fiddle

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

7 Comments

firebug is giving following error<br>TypeError: rows[i] is undefined rows[i].onclick = (function() {
Did that error happen after you added window.onload? Did you make any other changes to code given in question? Can you show me your current updated code? Because, the code should work (as can be seen in the fiddle demo).
because I am working on dialog box that also dynamically creating table. I am unable to use window.onload = deleteRow;(it is best suit if your table exist onLoad event) Although i tried but it won't work
You mean there is no table with id='filterTableBody' on initial page load? If yes, attach the Event handler to the rows within the function that creates the rows.
Also, you have a delete button but attaching the action to the whole row instead of just to the button. Is that what you really need?
|
0

This code works for me. Thank you @harry for pin pointing the problem.

function deleteRowUI(btndel) {
            var table=document.getElementById('filterTableBody');
            if (typeof(btndel) == "object") {
                p=btndel.parentNode.parentNode;
                p.parentNode.removeChild(p);
                var oTable = document.getElementById('filterTableBody');

                //gets rows of table
                var rowLength = oTable.rows.length;
                for (var i = 1; i < rowLength; i++){

                       var oCells = oTable.rows.item(i).cells;
                   //gets cells of current row
                   var cellLength = oCells.length-1;
                       for(var j = 0; j < cellLength; j++){
                           oCells.item(j).innerHTML = "";
                           break;
                       }
                       break;
            }
        } else {
            return false;
        }
    }

1 Comment

Good that you have managed to solve it mate. Mark the answer as accepted. Also, I don't think you need to get document.getElementById('filterTableBody') into two separate variables. One should be enough (remove the var table as it is unused) :)

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.