0

I'm new to dynamic javascript and I'm adding a button to my dynamic HTML table.

function addToTable(){

    var table = document.getElementById("table");
    var row = table.insertRow(counter + 1);

    var removeRow=document.createElement("BUTTON");

    var cell0 = row.insertCell(0)
    var cell1 = row.insertCell(1);
    var cell2 = row.insertCell(2);
    var cell3 = row.insertCell(3);

    cell0.innerHTML = counter + 1;
    cell1.innerHTML = getName(array[counter]); 
    cell2.innerHTML = getEmail(array[counter]);
    cell3.innerHTML = document.body.appendChild(removeRow);

    counter++;
}

Whenever I run this, in the first the cells I get the index number, the name, and the email. in the 4th cell it prints "[object HTMLButtonElement]".

And an addition issue is when I assign a label on the button the entire method doesn't work. This is how I was adding a label.

var removeRow=document.createElement("BUTTON");
var text=document.createTextNode("Remove");
btn.appendChild(text);
document.body.appendChild(removeRow);
3
  • Can you make a jsfiddle with your code? Commented Jul 25, 2014 at 23:36
  • Regarding this line: document.getElementById("table"); do you really have an element with id="table" in your HTML? Commented Jul 25, 2014 at 23:43
  • He probably does, because in his question he states that something other than what he expected is showing up inside the table Commented Jul 25, 2014 at 23:43

1 Answer 1

1

You shouldn't be setting the innerHTML of the cell to the return value of document.appendChild this returns an DOM object which is turned into a string when set as HTML. You should simply append the button to the cell.

cell3.appendChild( removeRow );

fiddle

Note: as I did in the fiddle, you should set the innerHTML of the created button.

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

Comments

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.