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 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 ... 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>

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

6 Comments

but i have more in my question how can i make it work for entering multiple times ?
I will edit my answer.
You can check my answer now.
nice ty can you explain how this works, i mean what did you changed in the js ?
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. :)
|
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 Comments

Oh yea but it changes the table heading
yeah that's different issue now :-)
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 Comments

but i have more in my question how can i make it work for entering multiple times ?
check last edit
yep ty it works

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.