0

i am appending a new row to the end of a table by using appendrow() , but it doesnt work , can anybody help me .

htm:

<body onload="makeTable()">
<table id= "tbl" border="1"></table>
<form>
<input type="button" value=" append a new row " onclick="appendRow()" /><br />
</form>

js:

 function makeTable(){
var theTable =document.getElementById("tbl");
if (theTable.firstChild != null)
     {
      var badIEBody = theTable.childNodes[0];  
      theTable.removeChild(badIEBody);
}
var tBody = document.createElement("TBODY");
theTable.appendChild(tBody);

var newRow = document.createElement("tr");
var c1 = document.createElement("td");
var v1 = document.createTextNode("HIT6307");
c1.appendChild(v1);
newRow.appendChild(c1);
var c2 = document.createElement("td");
var v2 = document.createTextNode("Internet Technology");
c2.appendChild(v2);
newRow.appendChild(c2);
tBody.appendChild(newRow);

}

function appendRow() {
  var code = prompt("What is the code of subject", "Type code here");
  var name = prompt("What is the name of subject", "Type name here");

  var tBody = document.getElementByTagName("TBODY");
  newRow = document.createElement("tr");
c1 = document.createElement("td");
v1 = document.createTextNode("code");
c1.appendChild(v1);
newRow.appendChild(c1);
c2 = document.createElement("td");
v2 = document.createTextNode("name");
c2.appendChild(v2);
newRow.appendChild(c2);
tBody.appendChild(newRow);

     tBody.appendChild(newRow);
}
3
  • 1
    what does console/jslint say?? Commented Jun 4, 2011 at 10:47
  • @DrStrangeLove its " document.getElementByTagName is not a function" Commented Jun 4, 2011 at 10:52
  • try document.getElementsByTagName Commented Jun 4, 2011 at 10:54

2 Answers 2

1

There is no function getElementByTagName in javascript but getElementsByTagName` (list of elements).

Also, you are filling the new text elements with string constants ("code" & "name") not the value of corresponding variables:

v1 = document.createTextNode("code");

-->
v1 = document.createTextNode(code);

You should write appendRow like the following:

function appendRow() {
    var code = prompt("What is the code of subject", "Type code here");
    var name = prompt("What is the name of subject", "Type name here");

    var tBody = document.getElementsByTagName("TBODY")[0];

    newRow = document.createElement("tr");
    c1 = document.createElement("td");
    v1 = document.createTextNode(code);
    c1.appendChild(v1);
    newRow.appendChild(c1);

    c2 = document.createElement("td");
    v2 = document.createTextNode(name);
    c2.appendChild(v2);
    newRow.appendChild(c2);

    tBody.appendChild(newRow);
}

Demo with row deletion: http://jsfiddle.net/Wqjhj/

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

1 Comment

@maniji i tried it deleting a row when i clicked on the button but how a row can be selected here?
1
function appendRow() {
  var code = prompt("What is the code of subject", "Type code here");
  var name = prompt("What is the name of subject", "Type name here");

  var tBody = document.getElementsByTagName("TBODY")[0];
  newRow = document.createElement("tr");
c1 = document.createElement("td");
v1 = document.createTextNode(code);
c1.appendChild(v1);
newRow.appendChild(c1);
c2 = document.createElement("td");
v2 = document.createTextNode(name);
c2.appendChild(v2);
newRow.appendChild(c2);
tBody.appendChild(newRow);

     tBody.appendChild(newRow);
}

1 Comment

you should remove quotes in createTextNode("code"); & createTextNode("name");

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.