Marc Intes
Marc Intes

Reputation: 737

Array contents would not display

I have created a javascript code that lets the user input something then it should display after the input but my display seems to not work at all, I am using document.getElementById to display the array content per input but it is not working.

Here is my complete code:

<html>
    <meta charset="UTF-8">
    <head>
        <title>Sample Array</title>
        <script>var index = 0;</script>
    </head> 

    <body>

        <p id = "display">
        Input:
        <input type="text" id="input">
        <input type="button" value="GO" onClick="press(input.value)">

        <script>

            var sample = new Array();

            function press(Number)
            {               
                    sample[index] = Number;
                    document.getElementById("display").InnerHTML += sample[index] + " ";
                    index++;                    
            }       

        </script>

    </body>
</html> 

What seems to be the problem? I am still learning the basics please bear with me.

Upvotes: 0

Views: 68

Answers (4)

K&#233;vin Isabelle
K&#233;vin Isabelle

Reputation: 66

The problem is with "InnerHTML". The correct syntax is "innerHTML" with small "i".

Upvotes: 1

user2575725
user2575725

Reputation:

First your <p> is not complete; <p id = "display"></p>

Second its:

document.getElementById("display").innerHTML += sample[index] + " ";

Upvotes: 1

mr rogers
mr rogers

Reputation: 3260

I made two tiny changes that seem to make this work.

First, close the <p> tag so that when you set innerHtml it does not include the inputs.

Second, change InnerHHTML to innerHTML. Methods and properties are case sensitive.

    <p id = "display"></p>
    Input:
    <input type="text" id="input">
    <input type="button" value="GO" onClick="press(input.value)">

    <script>

        var sample = new Array();

        function press(Number)
        {               
                sample[index] = Number;
                document.getElementById("display").innerHTML += sample[index] + " ";
                index++;                    
        }       

    </script>

Upvotes: 1

Antonio E.
Antonio E.

Reputation: 4391

change

document.getElementById("display").InnerHTML

to

document.getElementById("display").innerHTML

(note the lowercase i

Here's the fiddle

Upvotes: 1

Related Questions