0

I am new in Javascripting language. I tried to build an application in which , there is one HTML page from which I get single input entry by using Submit button, and stores in the container(data structure) and dynamically show that list i.e., list of strings, on the same page

means whenever I click submit button, that entry will automatically append on the existing list on the same page.

HTML FILE :-

<html>
    <head>
        <script type = "text/javascript" src = "operation_q_2.js"></script>
    </head>
    <body>
        Enter String : <input type= "text" name = "name" id = "name_id"/>
        <button type="button"   onClick = "addString(this.input)">Submit</button>
    </body>
</html> 

JAVASCRIPT CODE

var input = [];

function addString(x) {
    var s = document.getElementById("name_id").value;//x.name.value;
    input.push(input);
    var size = input.length;
    //alert(size);
    printArray(size);
}

function printArray(size){
    var div = document.createElement('div');
    for (var i = 0 ; i < size; ++i) {
        div.innerHTML += input[i] + "<br />";
    }
    document.body.appendChild(div);
    //alert(size);
}

Here it stores the strings in the input Array, but unable to show on the web page. Need help please.

Tell me one more thing there is one code on given link. It also not gives desired answer. Please help me overcome from this problem.

<html>
<body>
    <script>
       function addValue(a) {
            var element1 = document.createElement('tr');
            var element2 = document.createElement('td');
            var text = document.createTextNode(a);
            var table = document.getElementById('t');
            element2.appendChild(text);
            element1.appendChild(element2); 
            table.tBodies(0).appendChild(element1);
       }
   </script>
   Name: <input type="text" name="a">
   <input type="button" value="Add" onClick='javascript:addValue(a.value)'>
   <table id="t" border="1">
       <tr><th>Employee Name</th></tr>
   </table>
</body>
</html>
2
  • 1
    You should use the console : it shows the errors and console.log is much more convenient than alert. This will really make you more efficient. Commented Jan 19, 2013 at 18:05
  • This code print ` ' ` symbols then after submitting more, number quotes increses by one in every alternative line. Very strange behavior Commented Jan 19, 2013 at 18:09

1 Answer 1

3

In your code where you push an item to the end of your input array, you're trying to push the array instead of the value to the array. So if your problem is that your values aren't being appended to the page is because you're trying to append the array that's empty initially onto itself.

So instead of

input.push(input);

It should be

input.push(s);

Since "s" you already declared to be the value from the text field.

And if you're not going to use that parameter you're passing in, I would get rid of it.

References: Javascript Array.Push()

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

4 Comments

thanks a lot. I accepted your answer. How silly that mistake is :(
Tell me about the second question's answer. Why that code is not working ??
Try just table.appendChild(element1); You don't have any <tbody> elements within your table, so tBodies(0) doesn't return anything.
Now it starts entering value. But that value is UNDEFINED.

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.