1

I have created form. now I need to take input data into table. So I have created the code but it does not working. In here, I take the input-data by using id = "input1" then I click on the submit button and it will call for myfunction() function. That function will show the data in the table. But it not working.

<html>
<head>
    <title>
    </title>
</head>
<body>
<div id="form1">
</div>
<div id="table1">

</div>
<script>
            var f = document.createElement("form");

            var i = document.createElement("input"); 
            i.setAttribute('type',"text");
            i.setAttribute('name',"input1");
            i.setAttribute('id',"input1");

            var s = document.createElement("input");
            s.setAttribute('type',"submit");
            s.setAttribute('value',"Submit");
            s.setAttribute('id',"done")

            f.appendChild(i);
            f.appendChild(s);


            document.getElementById("form1").appendChild(f);
</script>   
<script>
    document.getElementById("done").onclick = function() {myFunction()};

    function myFunction() {
            var t = document.createElement("table");
            t.setAttribute('border',"1");

            var row = document.createElement("tr"); 
            var cell = document.createElement("td");
            var cellText = document.getElementById("input1");

            cell.appendChild(cellText);
            row.appendChild(cell);
            t.appendChild(row);

            document.getElementById("table1").appendChild(t);
    }

</script>
</body>
 </html>
1
  • whats the output now? Commented Jan 30, 2016 at 9:04

4 Answers 4

2

You need to update your code to following

document.getElementById("done").onclick = function() {
       event.preventDefault();
       myFunction()
};

For reference - http://plnkr.co/edit/ZgMWu6tCb95R9TEAQKbi?p=preview

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

Comments

0
var s = document.createElement("input");
        s.setAttribute('type',"button");
        s.setAttribute('value',"Submit");
        s.setAttribute('id',"done");

Change input type to button, as you are showing the data in a table you dont need submit button, if you are giving submit button it will automatically try to call the action which is not defined. If you want o use submit then add event.preventDefault(); to the myFunction()

Comments

0

You should change the type of button from submit to button. Also if you just need the text inside the input box take the value instead of the element.

 <html>
    <head>
        <title>
        </title>
    </head>
    <body>
    <div id="form1">
    </div>
    <div id="table1">
    <table id="myTable" border="1"></table>
    </div>
    <script>
                var f = document.createElement("form");

                var i = document.createElement("input"); 
                i.setAttribute('type',"text");
                i.setAttribute('name',"input1");
                i.setAttribute('id',"input1");

                var s = document.createElement("input");
                s.setAttribute('type',"button");
                s.setAttribute('value',"Submit");
                s.setAttribute('id',"done")

                f.appendChild(i);
                f.appendChild(s);


                document.getElementById("form1").appendChild(f);
    </script>   
    <script>
        document.getElementById("done").onclick = function() {myFunction()};

        function myFunction() {
                var table = document.getElementById("myTable");

                var row = table.insertRow(0);
            var cell1 = row.insertCell(0);
                var cellText = document.getElementById("input1").value;
            cell1.innerHTML = cellText;
        }

    </script>
    </body>
     </html>

Comments

0

Try this,

  • You should consider a decent framework such as jQuery / Angular / ... to do these HTML manipulations

I've changed the form input type="submit" to button type="button", added .value to the cellText var, used .innerHTML to set it inside the <td>

Online Demo - https://plnkr.co/edit/ApNX7vuN31Wu0fbCR9Vh?p=preview

  <script>
    var f = document.createElement("form");

    var i = document.createElement("input");
    i.setAttribute('type', "text");
    i.setAttribute('name', "input1");
    i.setAttribute('id', "input1");

    var s = document.createElement("button");
    s.setAttribute('type', "button");
    s.innerHTML = "Click me";
    s.setAttribute('id', "done")

    f.appendChild(i);
    f.appendChild(s);


    document.getElementById("form1").appendChild(f);
  </script>
  <script>
    document.getElementById("done").onclick = function() {
      myFunction()
    };

    function myFunction() {
      var t = document.createElement("table");
      t.setAttribute('border', "1");

      var row = document.createElement("tr");
      var cell = document.createElement("td");
      var cellText = document.getElementById("input1").value;

      cell.innerHTML = cellText;
      row.appendChild(cell);
      t.appendChild(row);

      document.getElementById("table1").appendChild(t);
    }
  </script>

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.