Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
57
rated 0 times [  62] [ 5]  / answers: 1 / hits: 23466  / 11 Years ago, fri, november 15, 2013, 12:00:00

I can't get the codes to work. Can somebody point out what have I done wrong? I just want to print the input to a table using JavaScript.



HTML



Item:
<input type=text name=item id=item /><br />
Quantity:
<input type=text name=quantity id=quantity /><br />
Price: AUD
<input type=text name=price id=price /><br /><br />
<input type=button value=Add Product + onClick=addRow() id=add><br /><br />

<table id=table border=1>
<tr>
<th>Item</th>
<th>Quantity</th>
<th>Price</th>
</tr>
</table>


JavaScript



function addRow() {
use strict;

var table = document.getElementById(table);
var td1 = document.createElement(td);
var td2 = document.createElement(td);
var td3 = document.createElement(td);

td1.innerHTML = document.getElementById(item).value;
td2.innerHTML = document.getElementById(quantity).value;
td3.innerHTML = document.getElementById(price).value;

row.appendChild(td1);
row.appendChild(td2);
row.appendChild(td3);

table.children[0].appendChild(row);
});

More From » html

 Answers
53

You are missing



var row= document.createElement(tr);


before the line



var td1 = document.createElement(td);


and in the end }); is a syntax error. replace it with }



Fiddle: http://jsfiddle.net/Sg2vD/


[#74273] Wednesday, November 13, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ellisc

Total Points: 533
Total Questions: 82
Total Answers: 90

Location: Bangladesh
Member since Thu, Aug 5, 2021
3 Years ago
;