iswg
iswg

Reputation: 57

Click Counter not working

This is a super basic counter which just counts the button clicks. Below is the body of my html.

 <body>
    <div class="wrapper" ><button id="but"> Click</button></div>
    <h2><div class="counter"> Counter: <span id = "countNum"> 0 </span></div></h2>
    <script type="text/javascript">
        var button = document.getElementById('but');
        var counter = document.getElementbyId('countNum');
        var count = 0;
        button.onClick = function() {
            count += 1;
            counter.innerHTML = count;
        };
    </script>
</body>

However, there is simply no change on my counter when I click the button. I've tried placing output statements in the start of the script but they don't show up either. Have I placed it wrong? Is it an error with my code?

I've gone through all the similar posts but cannot figure out my error.

Upvotes: 0

Views: 420

Answers (1)

asosnovsky
asosnovsky

Reputation: 2235

You misspelled quite a few functions here.

Here is the correct javascript code. Please note that its case sensitive, i.e. document.getElementbyId is not the same as document.getElementById, and button.onClick is not the same as button.onclick.

var button = document.getElementById('but');
var counter = document.getElementById('countNum');
var count = 0;
button.onclick = function() {
    count += 1;
    counter.innerHTML = count;
};

Upvotes: 3

Related Questions