0

Let's say i have a simple page that just contains a button that when clicked adds 1 to a java script num variable:

HTML:

<body>
<div>
<button type="button" onclick="someFunction()">Clicking here adds 1 to the "count" variable</button>
</div>
<br>
<div id="output"></div>
</body>

Javascript:

var count = new num;

someFunction() {
    count =+ 1;
}

So my question is what would you put into the js to display the "count" varible in the div with the id "output"? do you use innerHTML? Because that doesn't seem to work....

Also, is it possible with or without jQuery?

1 Answer 1

1

Why not? However, you have to note that number increment must be done with count += 1 or shorter count++. In the following example I have used prefix increment to do the calculation before assignment.

var count = 0;
function someFunction() {
    document.getElementById("output").innerHTML = ++count;
}

Additionally, consider initialising numeric variables with its' initial values as described above.

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

1 Comment

just tried it and it worked. i see where i screwed up...i didnt tell it to show the variable correctly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.