0

I have added a row using javascript. Now I want the new columns's value to be picked from the input box text by id. There's some sort of error in it, which I'm unable to get.

<html>
<head>
<script>
function addtosubtotal() {
    var table = document.getElementById("myTable");
    var row = table.insertRow(2);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    cell1.innerHTML = getElementById("cid");
    cell2.innerHTML = document.getElementById("name");
        }
</script>
</head>

<body>
<table id="myTable" >
    <tr>
        <td>S. No</td>
        <td>Item Name</td>
        <td>Subtotal</td>
    </tr>
    <tr>
        <td><input type="text" id="cid" name="id" /> </td>
        <td><button onclick="addtosubtotal()" /></td>
    </tr>
</table>
</body>
</html>
3
  • getElementById("cid") -> document.getElementById("id") + other things. Check this out: jsfiddle.net/wbddLfnz Commented Mar 11, 2015 at 19:33
  • that was a typing error.. corrected.. it isn't working yet.. Commented Mar 11, 2015 at 19:37
  • The jsfiddle link i gave you works. If it does not do what you want it to do, please edit your question and explain clearly what you want to do. Commented Mar 11, 2015 at 19:45

1 Answer 1

1

You don't have an id for the input name:

<input type="text" id="name" name="name" />

So you wont get it by id

You can also combine the two statements in your JavaScript if you want to

<script>
function addtosubtotal() {
    var table = document.getElementById("myTable");
    var row = table.insertRow(2);
    var cell1 = row.insertCell(0).innerHTML = document.getElementById("cid").value;
    var cell2 = row.insertCell(1).innerHTML = document.getElementById("name").value;
        }
</script>
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.