0

I want to have a table and a textbox, when I input some value for the textbox then click the button, the value will be added into a table. I wrote the code but it doesn't run. Please help me to find out what wrong with the code below.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script>
function send() {
        tbl=document.getElementById("idtable");
        tr = document.createElement("tr");
        td1 = document.createElement("td");
        td2 = document.createElement("td");
        name = document.getElementById("txtname").value;
        email = document.getElementById("txtemail").value;
        td1.innerText=name;
        td2.innerText=email;
        tr.appendChild(td1);
        tr.appendChild(td2);
        tbl.appendChild(tr);
}
</script>
</head>
<body>
<input type="text" id="txtname" name="txtname"/> <br/><br/>
<input type="text" id="txtemail" name="txtemail"/>
<p> <input type="button" value="Send" onclick="send()"/>
<table id="idtable" border="1">
<tr><td>Name</td> 
 <td>Email</td>
</tr>
</table>
</body>
</html>
7
  • Does browser JavaScript console say anything? Commented Oct 21, 2017 at 9:37
  • 1
    this code work fine what is he Problem ? Commented Oct 21, 2017 at 9:42
  • no, it doesn't! Commented Oct 21, 2017 at 9:43
  • I don't know why it doesn't work on my computer. Commented Oct 21, 2017 at 9:44
  • 1
    Can you be more specific than "it doesn't work" ? What is wrong when running this code on your computer ? Commented Oct 21, 2017 at 9:45

1 Answer 1

1

try this code:

function send(){
   var table=document.getElementById("table");
   name=document.getElementById("txtname").value;
   email=document.getElementById("txtemail").value;
   table.innerHTML+=('<tr><td>'+name+'</td><td>'+email+'</td></tr>')
}
<input id="txtname" name="txtname"/> <br/>
<input id="txtemail" name="txtemail"/> <br/>
<input type="button" value="Send" onclick="send()"/><br/>
<table id="table" border="1">
   <tr><td>Name</td><td>Email</td></tr>
</table>

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.