How to add new row in table from user input in HTML











up vote
0
down vote

favorite












I just want to build a website. It has a table, that shows data of each row. Basically, this table must be able to add new rows that contain new data, when user click add button.



This is my code:






function myFunction() {
var table = document.getElementById("myTable");
var row = table.insertRow(0);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = "NEW CELL1";
cell2.innerHTML = "NEW CELL2";
}

table,
td {
border: 1px solid black;
}

<p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

<table id="myTable">
<tr>
<td>Row1 cell1</td>
<td>Row1 cell2</td>
</tr>
<tr>
<td>Row2 cell1</td>
<td>Row2 cell2</td>
</tr>
<tr>
<td>Row3 cell1</td>
<td>Row3 cell2</td>
</tr>
</table>
<br>

<button onclick="myFunction()">Try it</button>





But the result of that code is, when user click add button, it always show "NEW CELL 1" and "NEW CELL 2". My question is, how to modify the code, especially:



cell1.innerHTML = "NEW CELL1";
cell2.innerHTML = "NEW CELL2";


So that, the table is possible to get input data from user?










share|improve this question




















  • 2




    Step 1: Create some inputs. Step 2 Read up on how to get value from an input element
    – charlietfl
    Nov 19 at 18:38

















up vote
0
down vote

favorite












I just want to build a website. It has a table, that shows data of each row. Basically, this table must be able to add new rows that contain new data, when user click add button.



This is my code:






function myFunction() {
var table = document.getElementById("myTable");
var row = table.insertRow(0);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = "NEW CELL1";
cell2.innerHTML = "NEW CELL2";
}

table,
td {
border: 1px solid black;
}

<p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

<table id="myTable">
<tr>
<td>Row1 cell1</td>
<td>Row1 cell2</td>
</tr>
<tr>
<td>Row2 cell1</td>
<td>Row2 cell2</td>
</tr>
<tr>
<td>Row3 cell1</td>
<td>Row3 cell2</td>
</tr>
</table>
<br>

<button onclick="myFunction()">Try it</button>





But the result of that code is, when user click add button, it always show "NEW CELL 1" and "NEW CELL 2". My question is, how to modify the code, especially:



cell1.innerHTML = "NEW CELL1";
cell2.innerHTML = "NEW CELL2";


So that, the table is possible to get input data from user?










share|improve this question




















  • 2




    Step 1: Create some inputs. Step 2 Read up on how to get value from an input element
    – charlietfl
    Nov 19 at 18:38















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I just want to build a website. It has a table, that shows data of each row. Basically, this table must be able to add new rows that contain new data, when user click add button.



This is my code:






function myFunction() {
var table = document.getElementById("myTable");
var row = table.insertRow(0);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = "NEW CELL1";
cell2.innerHTML = "NEW CELL2";
}

table,
td {
border: 1px solid black;
}

<p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

<table id="myTable">
<tr>
<td>Row1 cell1</td>
<td>Row1 cell2</td>
</tr>
<tr>
<td>Row2 cell1</td>
<td>Row2 cell2</td>
</tr>
<tr>
<td>Row3 cell1</td>
<td>Row3 cell2</td>
</tr>
</table>
<br>

<button onclick="myFunction()">Try it</button>





But the result of that code is, when user click add button, it always show "NEW CELL 1" and "NEW CELL 2". My question is, how to modify the code, especially:



cell1.innerHTML = "NEW CELL1";
cell2.innerHTML = "NEW CELL2";


So that, the table is possible to get input data from user?










share|improve this question















I just want to build a website. It has a table, that shows data of each row. Basically, this table must be able to add new rows that contain new data, when user click add button.



This is my code:






function myFunction() {
var table = document.getElementById("myTable");
var row = table.insertRow(0);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = "NEW CELL1";
cell2.innerHTML = "NEW CELL2";
}

table,
td {
border: 1px solid black;
}

<p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

<table id="myTable">
<tr>
<td>Row1 cell1</td>
<td>Row1 cell2</td>
</tr>
<tr>
<td>Row2 cell1</td>
<td>Row2 cell2</td>
</tr>
<tr>
<td>Row3 cell1</td>
<td>Row3 cell2</td>
</tr>
</table>
<br>

<button onclick="myFunction()">Try it</button>





But the result of that code is, when user click add button, it always show "NEW CELL 1" and "NEW CELL 2". My question is, how to modify the code, especially:



cell1.innerHTML = "NEW CELL1";
cell2.innerHTML = "NEW CELL2";


So that, the table is possible to get input data from user?






function myFunction() {
var table = document.getElementById("myTable");
var row = table.insertRow(0);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = "NEW CELL1";
cell2.innerHTML = "NEW CELL2";
}

table,
td {
border: 1px solid black;
}

<p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

<table id="myTable">
<tr>
<td>Row1 cell1</td>
<td>Row1 cell2</td>
</tr>
<tr>
<td>Row2 cell1</td>
<td>Row2 cell2</td>
</tr>
<tr>
<td>Row3 cell1</td>
<td>Row3 cell2</td>
</tr>
</table>
<br>

<button onclick="myFunction()">Try it</button>





function myFunction() {
var table = document.getElementById("myTable");
var row = table.insertRow(0);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = "NEW CELL1";
cell2.innerHTML = "NEW CELL2";
}

table,
td {
border: 1px solid black;
}

<p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

<table id="myTable">
<tr>
<td>Row1 cell1</td>
<td>Row1 cell2</td>
</tr>
<tr>
<td>Row2 cell1</td>
<td>Row2 cell2</td>
</tr>
<tr>
<td>Row3 cell1</td>
<td>Row3 cell2</td>
</tr>
</table>
<br>

<button onclick="myFunction()">Try it</button>






javascript jquery html5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 20:20









Towkir Ahmed

922619




922619










asked Nov 19 at 18:32









Agnes Palit

1639




1639








  • 2




    Step 1: Create some inputs. Step 2 Read up on how to get value from an input element
    – charlietfl
    Nov 19 at 18:38
















  • 2




    Step 1: Create some inputs. Step 2 Read up on how to get value from an input element
    – charlietfl
    Nov 19 at 18:38










2




2




Step 1: Create some inputs. Step 2 Read up on how to get value from an input element
– charlietfl
Nov 19 at 18:38






Step 1: Create some inputs. Step 2 Read up on how to get value from an input element
– charlietfl
Nov 19 at 18:38














2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










Simply take your input value and set it as the html value for the target cells






function myFunction() {
var table = document.getElementById("myTable");
var row = table.insertRow(0);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = document.getElementById('cellOne').value;
cell2.innerHTML = document.getElementById('cellTwo').value;
}

table,
td {
border: 1px solid black;
}

<p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

<table id="myTable">
<tr>
<td>Row1 cell1</td>
<td>Row1 cell2</td>
</tr>
<tr>
<td>Row2 cell1</td>
<td>Row2 cell2</td>
</tr>
<tr>
<td>Row3 cell1</td>
<td>Row3 cell2</td>
</tr>
</table>
<br>

<input type='text' id='cellOne' />
<input type='text' id='cellTwo' />

<button onclick="myFunction()">Try it</button>








share|improve this answer





















  • hi, thank you for the answer, however i have another code version, that i really curious how to solve that, can you help me? i want to post in new post, but i realize i can't create same title @Towkir Ahmed
    – Agnes Palit
    Nov 19 at 19:00








  • 1




    you can create a different question if that is a different problem, you can comment the link here. but if it's about same problem, it might be marked as duplicate by moderators.
    – Towkir Ahmed
    Nov 19 at 19:02




















up vote
1
down vote













You need some form inputs to allow you to enter text, here is a very basic example to get you started:






function myFunction() {
var table = document.getElementById("myTable");
var row = table.insertRow(0);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = document.querySelector('#newCellOneText').value;
cell2.innerHTML = document.querySelector('#newCellTwoText').value;
}

table,
td {
border: 1px solid black;
}

<p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

<table id="myTable">
<tr>
<td>Row1 cell1</td>
<td>Row1 cell2</td>
</tr>
<tr>
<td>Row2 cell1</td>
<td>Row2 cell2</td>
</tr>
<tr>
<td>Row3 cell1</td>
<td>Row3 cell2</td>
</tr>
</table>
<br>

Cell 1 Text: <input type="text" id="newCellOneText" /><br/>
Cell 2 Text: <input type="text" id="newCellTwoText" /><br/>
<button onclick="myFunction()">Try it</button>








share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380689%2fhow-to-add-new-row-in-table-from-user-input-in-html%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    Simply take your input value and set it as the html value for the target cells






    function myFunction() {
    var table = document.getElementById("myTable");
    var row = table.insertRow(0);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    cell1.innerHTML = document.getElementById('cellOne').value;
    cell2.innerHTML = document.getElementById('cellTwo').value;
    }

    table,
    td {
    border: 1px solid black;
    }

    <p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

    <table id="myTable">
    <tr>
    <td>Row1 cell1</td>
    <td>Row1 cell2</td>
    </tr>
    <tr>
    <td>Row2 cell1</td>
    <td>Row2 cell2</td>
    </tr>
    <tr>
    <td>Row3 cell1</td>
    <td>Row3 cell2</td>
    </tr>
    </table>
    <br>

    <input type='text' id='cellOne' />
    <input type='text' id='cellTwo' />

    <button onclick="myFunction()">Try it</button>








    share|improve this answer





















    • hi, thank you for the answer, however i have another code version, that i really curious how to solve that, can you help me? i want to post in new post, but i realize i can't create same title @Towkir Ahmed
      – Agnes Palit
      Nov 19 at 19:00








    • 1




      you can create a different question if that is a different problem, you can comment the link here. but if it's about same problem, it might be marked as duplicate by moderators.
      – Towkir Ahmed
      Nov 19 at 19:02

















    up vote
    2
    down vote



    accepted










    Simply take your input value and set it as the html value for the target cells






    function myFunction() {
    var table = document.getElementById("myTable");
    var row = table.insertRow(0);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    cell1.innerHTML = document.getElementById('cellOne').value;
    cell2.innerHTML = document.getElementById('cellTwo').value;
    }

    table,
    td {
    border: 1px solid black;
    }

    <p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

    <table id="myTable">
    <tr>
    <td>Row1 cell1</td>
    <td>Row1 cell2</td>
    </tr>
    <tr>
    <td>Row2 cell1</td>
    <td>Row2 cell2</td>
    </tr>
    <tr>
    <td>Row3 cell1</td>
    <td>Row3 cell2</td>
    </tr>
    </table>
    <br>

    <input type='text' id='cellOne' />
    <input type='text' id='cellTwo' />

    <button onclick="myFunction()">Try it</button>








    share|improve this answer





















    • hi, thank you for the answer, however i have another code version, that i really curious how to solve that, can you help me? i want to post in new post, but i realize i can't create same title @Towkir Ahmed
      – Agnes Palit
      Nov 19 at 19:00








    • 1




      you can create a different question if that is a different problem, you can comment the link here. but if it's about same problem, it might be marked as duplicate by moderators.
      – Towkir Ahmed
      Nov 19 at 19:02















    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    Simply take your input value and set it as the html value for the target cells






    function myFunction() {
    var table = document.getElementById("myTable");
    var row = table.insertRow(0);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    cell1.innerHTML = document.getElementById('cellOne').value;
    cell2.innerHTML = document.getElementById('cellTwo').value;
    }

    table,
    td {
    border: 1px solid black;
    }

    <p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

    <table id="myTable">
    <tr>
    <td>Row1 cell1</td>
    <td>Row1 cell2</td>
    </tr>
    <tr>
    <td>Row2 cell1</td>
    <td>Row2 cell2</td>
    </tr>
    <tr>
    <td>Row3 cell1</td>
    <td>Row3 cell2</td>
    </tr>
    </table>
    <br>

    <input type='text' id='cellOne' />
    <input type='text' id='cellTwo' />

    <button onclick="myFunction()">Try it</button>








    share|improve this answer












    Simply take your input value and set it as the html value for the target cells






    function myFunction() {
    var table = document.getElementById("myTable");
    var row = table.insertRow(0);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    cell1.innerHTML = document.getElementById('cellOne').value;
    cell2.innerHTML = document.getElementById('cellTwo').value;
    }

    table,
    td {
    border: 1px solid black;
    }

    <p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

    <table id="myTable">
    <tr>
    <td>Row1 cell1</td>
    <td>Row1 cell2</td>
    </tr>
    <tr>
    <td>Row2 cell1</td>
    <td>Row2 cell2</td>
    </tr>
    <tr>
    <td>Row3 cell1</td>
    <td>Row3 cell2</td>
    </tr>
    </table>
    <br>

    <input type='text' id='cellOne' />
    <input type='text' id='cellTwo' />

    <button onclick="myFunction()">Try it</button>








    function myFunction() {
    var table = document.getElementById("myTable");
    var row = table.insertRow(0);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    cell1.innerHTML = document.getElementById('cellOne').value;
    cell2.innerHTML = document.getElementById('cellTwo').value;
    }

    table,
    td {
    border: 1px solid black;
    }

    <p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

    <table id="myTable">
    <tr>
    <td>Row1 cell1</td>
    <td>Row1 cell2</td>
    </tr>
    <tr>
    <td>Row2 cell1</td>
    <td>Row2 cell2</td>
    </tr>
    <tr>
    <td>Row3 cell1</td>
    <td>Row3 cell2</td>
    </tr>
    </table>
    <br>

    <input type='text' id='cellOne' />
    <input type='text' id='cellTwo' />

    <button onclick="myFunction()">Try it</button>





    function myFunction() {
    var table = document.getElementById("myTable");
    var row = table.insertRow(0);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    cell1.innerHTML = document.getElementById('cellOne').value;
    cell2.innerHTML = document.getElementById('cellTwo').value;
    }

    table,
    td {
    border: 1px solid black;
    }

    <p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

    <table id="myTable">
    <tr>
    <td>Row1 cell1</td>
    <td>Row1 cell2</td>
    </tr>
    <tr>
    <td>Row2 cell1</td>
    <td>Row2 cell2</td>
    </tr>
    <tr>
    <td>Row3 cell1</td>
    <td>Row3 cell2</td>
    </tr>
    </table>
    <br>

    <input type='text' id='cellOne' />
    <input type='text' id='cellTwo' />

    <button onclick="myFunction()">Try it</button>






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 19 at 18:38









    Towkir Ahmed

    922619




    922619












    • hi, thank you for the answer, however i have another code version, that i really curious how to solve that, can you help me? i want to post in new post, but i realize i can't create same title @Towkir Ahmed
      – Agnes Palit
      Nov 19 at 19:00








    • 1




      you can create a different question if that is a different problem, you can comment the link here. but if it's about same problem, it might be marked as duplicate by moderators.
      – Towkir Ahmed
      Nov 19 at 19:02




















    • hi, thank you for the answer, however i have another code version, that i really curious how to solve that, can you help me? i want to post in new post, but i realize i can't create same title @Towkir Ahmed
      – Agnes Palit
      Nov 19 at 19:00








    • 1




      you can create a different question if that is a different problem, you can comment the link here. but if it's about same problem, it might be marked as duplicate by moderators.
      – Towkir Ahmed
      Nov 19 at 19:02


















    hi, thank you for the answer, however i have another code version, that i really curious how to solve that, can you help me? i want to post in new post, but i realize i can't create same title @Towkir Ahmed
    – Agnes Palit
    Nov 19 at 19:00






    hi, thank you for the answer, however i have another code version, that i really curious how to solve that, can you help me? i want to post in new post, but i realize i can't create same title @Towkir Ahmed
    – Agnes Palit
    Nov 19 at 19:00






    1




    1




    you can create a different question if that is a different problem, you can comment the link here. but if it's about same problem, it might be marked as duplicate by moderators.
    – Towkir Ahmed
    Nov 19 at 19:02






    you can create a different question if that is a different problem, you can comment the link here. but if it's about same problem, it might be marked as duplicate by moderators.
    – Towkir Ahmed
    Nov 19 at 19:02














    up vote
    1
    down vote













    You need some form inputs to allow you to enter text, here is a very basic example to get you started:






    function myFunction() {
    var table = document.getElementById("myTable");
    var row = table.insertRow(0);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    cell1.innerHTML = document.querySelector('#newCellOneText').value;
    cell2.innerHTML = document.querySelector('#newCellTwoText').value;
    }

    table,
    td {
    border: 1px solid black;
    }

    <p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

    <table id="myTable">
    <tr>
    <td>Row1 cell1</td>
    <td>Row1 cell2</td>
    </tr>
    <tr>
    <td>Row2 cell1</td>
    <td>Row2 cell2</td>
    </tr>
    <tr>
    <td>Row3 cell1</td>
    <td>Row3 cell2</td>
    </tr>
    </table>
    <br>

    Cell 1 Text: <input type="text" id="newCellOneText" /><br/>
    Cell 2 Text: <input type="text" id="newCellTwoText" /><br/>
    <button onclick="myFunction()">Try it</button>








    share|improve this answer

























      up vote
      1
      down vote













      You need some form inputs to allow you to enter text, here is a very basic example to get you started:






      function myFunction() {
      var table = document.getElementById("myTable");
      var row = table.insertRow(0);
      var cell1 = row.insertCell(0);
      var cell2 = row.insertCell(1);
      cell1.innerHTML = document.querySelector('#newCellOneText').value;
      cell2.innerHTML = document.querySelector('#newCellTwoText').value;
      }

      table,
      td {
      border: 1px solid black;
      }

      <p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

      <table id="myTable">
      <tr>
      <td>Row1 cell1</td>
      <td>Row1 cell2</td>
      </tr>
      <tr>
      <td>Row2 cell1</td>
      <td>Row2 cell2</td>
      </tr>
      <tr>
      <td>Row3 cell1</td>
      <td>Row3 cell2</td>
      </tr>
      </table>
      <br>

      Cell 1 Text: <input type="text" id="newCellOneText" /><br/>
      Cell 2 Text: <input type="text" id="newCellTwoText" /><br/>
      <button onclick="myFunction()">Try it</button>








      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        You need some form inputs to allow you to enter text, here is a very basic example to get you started:






        function myFunction() {
        var table = document.getElementById("myTable");
        var row = table.insertRow(0);
        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        cell1.innerHTML = document.querySelector('#newCellOneText').value;
        cell2.innerHTML = document.querySelector('#newCellTwoText').value;
        }

        table,
        td {
        border: 1px solid black;
        }

        <p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

        <table id="myTable">
        <tr>
        <td>Row1 cell1</td>
        <td>Row1 cell2</td>
        </tr>
        <tr>
        <td>Row2 cell1</td>
        <td>Row2 cell2</td>
        </tr>
        <tr>
        <td>Row3 cell1</td>
        <td>Row3 cell2</td>
        </tr>
        </table>
        <br>

        Cell 1 Text: <input type="text" id="newCellOneText" /><br/>
        Cell 2 Text: <input type="text" id="newCellTwoText" /><br/>
        <button onclick="myFunction()">Try it</button>








        share|improve this answer












        You need some form inputs to allow you to enter text, here is a very basic example to get you started:






        function myFunction() {
        var table = document.getElementById("myTable");
        var row = table.insertRow(0);
        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        cell1.innerHTML = document.querySelector('#newCellOneText').value;
        cell2.innerHTML = document.querySelector('#newCellTwoText').value;
        }

        table,
        td {
        border: 1px solid black;
        }

        <p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

        <table id="myTable">
        <tr>
        <td>Row1 cell1</td>
        <td>Row1 cell2</td>
        </tr>
        <tr>
        <td>Row2 cell1</td>
        <td>Row2 cell2</td>
        </tr>
        <tr>
        <td>Row3 cell1</td>
        <td>Row3 cell2</td>
        </tr>
        </table>
        <br>

        Cell 1 Text: <input type="text" id="newCellOneText" /><br/>
        Cell 2 Text: <input type="text" id="newCellTwoText" /><br/>
        <button onclick="myFunction()">Try it</button>








        function myFunction() {
        var table = document.getElementById("myTable");
        var row = table.insertRow(0);
        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        cell1.innerHTML = document.querySelector('#newCellOneText').value;
        cell2.innerHTML = document.querySelector('#newCellTwoText').value;
        }

        table,
        td {
        border: 1px solid black;
        }

        <p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

        <table id="myTable">
        <tr>
        <td>Row1 cell1</td>
        <td>Row1 cell2</td>
        </tr>
        <tr>
        <td>Row2 cell1</td>
        <td>Row2 cell2</td>
        </tr>
        <tr>
        <td>Row3 cell1</td>
        <td>Row3 cell2</td>
        </tr>
        </table>
        <br>

        Cell 1 Text: <input type="text" id="newCellOneText" /><br/>
        Cell 2 Text: <input type="text" id="newCellTwoText" /><br/>
        <button onclick="myFunction()">Try it</button>





        function myFunction() {
        var table = document.getElementById("myTable");
        var row = table.insertRow(0);
        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        cell1.innerHTML = document.querySelector('#newCellOneText').value;
        cell2.innerHTML = document.querySelector('#newCellTwoText').value;
        }

        table,
        td {
        border: 1px solid black;
        }

        <p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

        <table id="myTable">
        <tr>
        <td>Row1 cell1</td>
        <td>Row1 cell2</td>
        </tr>
        <tr>
        <td>Row2 cell1</td>
        <td>Row2 cell2</td>
        </tr>
        <tr>
        <td>Row3 cell1</td>
        <td>Row3 cell2</td>
        </tr>
        </table>
        <br>

        Cell 1 Text: <input type="text" id="newCellOneText" /><br/>
        Cell 2 Text: <input type="text" id="newCellTwoText" /><br/>
        <button onclick="myFunction()">Try it</button>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 18:38









        Stuart

        4,86021427




        4,86021427






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380689%2fhow-to-add-new-row-in-table-from-user-input-in-html%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            RAC Tourist Trophy