0

Please I want to extract the value of the input form with id input-a but the program keeps writing c instead of the value entered by the user. Please help. This is the JavaScript code.

< body>
<script>
function tableCreate() {
var body = document.body,
tbl = document.createElement('table');
tbl.style.width = '100px';
tbl.style.border = '2px solid black';
  var n = 5
  for (var i = 0; i < n; i++) {
    var tr = tbl.insertRow();                                                      
    var td =tr.insertCell(0);
    var tf = tr.insertCell(0);
    var input = document.createElement('input');
    input.name = "input-a" + i;
    input.id = "input-a" + i;
    input.value = "";
    var clone = input.cloneNode();
    clone.name = "input-b" + i;
    clone.id = "input-b" + i;
    td.appendChild(clone);
    tf.appendChild(input);
    td.style.border = '2px solid black';
    tf.style.border = '2px solid black';
}
  var form = document.createElement("form");
  form.appendChild(tbl);
  body.appendChild(form);
  var submit = document.createElement("input");
  submit.type = "submit";
  form.appendChild(submit)
  Var c= document.getElementById("input-a1)
  document.write('c'). }
  tableCreate(); 
 </script>
 </body >
chika
  • 177
  • 1
  • 3
  • 12
  • selection.value, selection being the input. So for example, document.getElementById('input-a1').value – thatOneGuy May 16 '16 at 09:39
  • Possible duplicate of [JavaScript get input text value](http://stackoverflow.com/questions/11563638/javascript-get-input-text-value) – thatOneGuy May 16 '16 at 09:40
  • you a missing a quotation mark here `Var c= document.getElementById("input-a1)` it should be `Var c= document.getElementById("input-a1")` also remove quotation marks here `document.write('c')` If it does not start working after you fixed the code let us know so we can help :) – Vladimir Drenovski May 16 '16 at 09:45
  • 1
    Once you've done the above, to get the correct value, `document.write('c')` will output the character "c", as you say. It should be `document.write(c)` (notice no quotes around the c) – Reinstate Monica Cellio May 16 '16 at 09:47

2 Answers2

1

code is writing 'c' because you type

document.write('c')

if you want to have value of c you should type

document.write(c.value);

but to have it working you should set this value in function which handles forms submit not after generating form...because now value is empty string

here you have working example https://jsfiddle.net/g8rschsu/

Kamyk.pl
  • 309
  • 2
  • 8
  • I tried it but when the page loads, the table shows followed by the string "undefined". Even after entering the numbers in input boxes and clicking submit. The page just simply reloads instead of showing the value of the input with id input-a1 – chika May 16 '16 at 10:36
1

This should work.

<script>
    window.onload = function () {
        function tableCreate() {
            var body = document.body;
            var tbl = document.createElement('table');
            tbl.style.width = '100px';
            tbl.style.border = '2px solid black';
            var n = 5;
            for (var i = 0; i < n; i++) {
                var tr = tbl.insertRow();
                var td = tr.insertCell(0);
                var tf = tr.insertCell(0);
                var input = document.createElement('input');
                input.name = "input-a" + i;
                input.id = "input-a" + i;
                input.value = "test";
                var clone = input.cloneNode();
                clone.name = "input-b" + i;
                clone.id = "input-b" + i;
                td.appendChild(clone);
                tf.appendChild(input);
                td.style.border = '2px solid black';
                tf.style.border = '2px solid black';
            }
            var form = document.createElement("form");
            form.appendChild(tbl);
            body.appendChild(form);
            var submit = document.createElement("input");
            submit.type = "submit";
            form.appendChild(submit)
            var c = document.getElementById("input-a1").value;
            document.write(c);
        };
        tableCreate();
    };
 </script>

I saw you did document.write('c') with quotes, this will write the string c and not the variable c.

Also you have to take the value of the input and not the input itself.

var c = document.getElementById("input-a1").value;

Ralf D'hooge
  • 262
  • 3
  • 7