Reputation: 3
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?
Upvotes: 0
Views: 4292
Reputation: 145378
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.
Upvotes: 1