1

How can i add a content to html table with input fields. For example here is html code:

        function add(){
            var name = document.getElementById("name");
            var surname = document.getElementById("surname");
            var output = document.getElementById("output");
            output.innerHTML = "<tr><td>"+name+"</td><td>"+surname+"</td></tr>"


        }
  

  <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial- scale=1.0">
        <title>Document</title>
        <style>
            table,td{
                border: 1px solid black;
                border-collapse: collapse;
            }
        </style>
    </head>
    <body>
    
        <form action="">
            <div>
                <label for="name">Name</label>
                <input type="text" id="name">
            </div>
            <div>
                <label for="name">Surname</label>
                <input type="text" id="surname">
            </div>
        </form>
        <input type="button" onclick="add();" value="Add">
        <div>
            <table id="output">
                <thead><td>Name</td><td>Surname</td></thead>
                <tbody></tbody>
            </table>
        </div>
        
    
    </body>
    </html>
I want to output my input fields in the table like rows.. but it does not work i dont know where is my problem i get [object HTMLInputElement]. And how can i make it work for entering more values because like this i can only enter one row

4
  • name.value and surname.value. name and surname are HTML elements represented as JS Objects. Commented Nov 21, 2021 at 14:42
  • How can i fix not changing the table heading
    – itmemilan
    Commented Nov 21, 2021 at 14:44
  • Put the id on the tbody instead of table. Commented Nov 21, 2021 at 14:45
  • Another problem now i can only input one item i want to store the previous items ...
    – itmemilan
    Commented Nov 21, 2021 at 14:46

3 Answers 3

2

How about using this code? It adds surname and name below the thead.

function add(){
  var name = document.getElementById("name");
  var surname = document.getElementById("surname");
  var output = document.querySelector("#output tbody");
  output.innerHTML += "<tr><td>"+name.value+"</td><td>"+surname.value+"</td></tr>"
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial- scale=1.0">
    <title>Document</title>
    <style>
        table,td{
            border: 1px solid black;
            border-collapse: collapse;
        }
    </style>
</head>
<body>
    <form action="">
        <div>
            <label for="name">Name</label>
            <input type="text" id="name">
        </div>
        <div>
            <label for="name">Surname</label>
            <input type="text" id="surname">
        </div>
    </form>
    <input type="button" onclick="add();" value="Add">
    <div>
        <table id="output">
            <thead><td>Name</td><td>Surname</td></thead>
            <tbody></tbody>
        </table>
    </div>
</body>
</html>

6
  • but i have more in my question how can i make it work for entering multiple times ?
    – itmemilan
    Commented Nov 21, 2021 at 14:52
  • I will edit my answer.
    – Ihor
    Commented Nov 21, 2021 at 14:53
  • You can check my answer now.
    – Ihor
    Commented Nov 21, 2021 at 14:53
  • nice ty can you explain how this works, i mean what did you changed in the js ?
    – itmemilan
    Commented Nov 21, 2021 at 14:54
  • 1
    output.innerHTML = "<tr><td>"+name.value+"</td><td>"+surname.value+"</td></tr>" this is first code. and output.innerHTML += "<tr><td>"+name.value+"</td><td>"+surname.value+"</td></tr>" this is code now. Change is +. This code adds new line to your table. :)
    – Ihor
    Commented Nov 21, 2021 at 14:56
1

you should assign the value of the input fields like below.

 function add(){
            var name = document.getElementById("name");
            var surname = document.getElementById("surname");
            var output = document.getElementById("output");
            output.innerHTML = "<tr><td>"+name.value+"</td><td>"+surname.value+"</td></tr>"
        }
2
  • Oh yea but it changes the table heading
    – itmemilan
    Commented Nov 21, 2021 at 14:44
  • yeah that's different issue now :-) Commented Nov 21, 2021 at 14:45
1

Try: Give tbody an id to avoid removing thead

<tbody id="output2"></tbody>
function add(){
  var name = document.getElementById("name");
  var surname = document.getElementById("surname");
  var output = document.getElementById("output2");
  output.innerHTML = "<tr><td>" + name.value + "</td><td>" + surname.value + "</td></tr>";
}

You are trying to insert an HTML input element instead of the value of the element. To make multiple entries possible try the last line of the function to:

output.innerHTML += "<tr><td>" + name.value + "</td><td>" + surname.value + "</td></tr>";
3
  • but i have more in my question how can i make it work for entering multiple times ?
    – itmemilan
    Commented Nov 21, 2021 at 14:50
  • check last edit
    – Peter Orji
    Commented Nov 21, 2021 at 14:55
  • yep ty it works
    – itmemilan
    Commented Nov 21, 2021 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.