2

Supposed to insert entered text between input field and send button, but it does noting.

<body>                              
    <section>
        <p>passcode: <input type=text id=passcode></p>
        <section id=middle></section>
        <p><input type=button id=button value=send></p>
    </section>
    <script type="text/javascript">
        var log=new Array();
        document.getElementById("button").onclick=exlog();
        exlog(){
            log.push(document.getElementById("passcode").value)
            for(i=0;i<log.length;i++){
            document.getElementById("middle").innerHtml=log[i];
        }
    }
    </script>
</body>

4 Answers 4

3

Change

        document.getElementById("middle").innerHtml=log[i];

to

        document.getElementById("middle").innerHTML=log[i];

Reference: https://developer.mozilla.org/en-US/docs/DOM/element.innerHTML

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

Comments

2

Your exlog function is missing the function keyword and you are setting the onclick handler incorrectly.

Also innerHtml should be innerHTML

    document.getElementById("button").onclick=exlog;
    function exlog(){
        log.push(document.getElementById("passcode").value)
        for(i=0;i<log.length;i++){
        document.getElementById("middle").innerHTML=log[i];
    }

1 Comment

@user1656125 You called the exlog function instead of assigning it to the onclick property
1

I noticed a couple of problems with your code. Most importantly, you should use innerHTML, not innerHtml (note capitalization).

Additionally, you need to be careful of how you deal with functions. Just putting exlog(){} will not create a function. Instead, you should use the function keyword.

Finally, you want to set the onclick handler to the function exlog. What you have used will actually set the onclick handler to the result of evaluating the exlog function.

Here's a solution with all of the suggested changes:

<body>                              
<section>
    <p>passcode: <input type=text id=passcode></p>
    <section id=middle></section>
    <p><input type=button id=button value=send></p>
</section>
<script type="text/javascript">
    var log=new Array();
    document.getElementById("button").onclick=exlog;
    function exlog(){
        log.push(document.getElementById("passcode").value)
        for(i=0;i<log.length;i++){
alert(log[i]);
        document.getElementById("middle").innerHTML=log[i];
    }
}
</script>
</body>

2 Comments

Use of new Array() is considered bad style now. Better off to say var log = []; instead. In particular, most find the code easier to read and the result of new Array(10) construct can be ... surprising.
Also, the values of the attributes of the html elements should be quoted.
0

You are evaluating the function by using it like this: exlog(), since exlog doesn't have a return statement, it evaluates to undefined, which the browser won't execute.

If you want to assign it you can just use it's name exlog

Also, as mentioned by Teemu and Musa, the function keyword was missing from the definition of exlog

Also, the capitalization of innerHTML has to be exact, mixing cases wont work.

<body>                              
    <section>
        <p>passcode: <input type=text id=passcode></p>
        <section id=middle></section>
        <p><input type=button id=button value=send></p>
    </section>
    <script type="text/javascript">
        var log = [];
        document.getElementById("button").onclick = exlog; // <== no need to evaluate the function, just assign it.
        function exlog(){ // <== as pointed out below function keyword was missing
            log.push(document.getElementById("passcode").value);
            document.getElementById("middle").innerHTML += log[log.length - 1]; // <== needs to be capitilized exactly like this
        }
    </script>
</body>

3 Comments

Thanks! I made the change and it works. But I don't get what you mean by the browser won't execute a function without a return, isn't that what we're doing here with the fix?
What I mean is that if you run the function like this: exlog(), it's implicit return value is undefined which the browser doesn't know how to execute.
I understand that it's return value is undefined but the function executes, is this just something semantic I'm misunderstanding? And when I click send it replaces what was previously sent. Do you know how to concatenate multiple entries. THANKS I REALLY APPRECIATE IT.

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.