Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Charles Gray
19,470 Pointsdoes anyone know how to set up a button that increases count every time it's click?
I thought there would be an increment option in mysql
1 Answer
Bryan Heredia
7,777 PointsI will show you the code first, then i will try to explain how it works
Here is the HTML
<button id="btn">You have clicked this button <span id="btn-counter"></span></button>
Basic HTML5
Here is the Javascript
// the button
var btn = document.getElementById("btn");
// the counter
var counter = document.getElementById("btn-counter");
// set the counter to 0
counter.innerHTML = 0;
// increment the counter by 1 everytime the user clicks on the button
btn.onclick = function()
{
counter.innerHTML++;
};
The btn variable in the javascript code contains the HTML button object.
The counter in the javascript code contains the counter that will be incrementing every time you click. In this case
the counter is the HTML <span id="btn-counter"></span> object.
we then set the btn_counter to 0
when the make the counter increment by 1 everytime we click on it by using the ++ operator on the counter.innerHTML variable.
I tried my best to explain this to you, I hope you understood. Good Luck !