javascript - Count on click button1 and recount if on click button2 -
i have 2 buttons , counter, have reset counter every time change button. don't know how reset counter.
var count = 0; var button1 = document.getelementbyid("button1"); var button2 = document.getelementbyid("button2"); var display = document.getelementbyid("displaycount"); function clickcount(){ count++; display.innerhtml = count; } button1.onclick = function(){ clickcount(); count=0; } button2.onclick = function(){ clickcount(); }
<input type="button" value="button1" id="button1" /> <input type="button" value="button2" id="button2" /> <p>clicks: <span id="displaycount">0</span> times.</p>
pass parameter clickcount
function button name, , check if has changed.
var count = 0; var lastbuttonclicked = ""; var button1 = document.getelementbyid("button1"); var button2 = document.getelementbyid("button2"); var display = document.getelementbyid("displaycount"); function clickcount(buttonname){ if (buttonname === lastbuttonclicked) { count++; } else { count = 1; lastbuttonclicked = buttonname; } display.innerhtml = count; } button1.onclick = function(){ clickcount("1"); } button2.onclick = function(){ clickcount("2"); }
<input type="button" value="button1" id="button1" /> <input type="button" value="button2" id="button2" /> <p>clicks: <span id="displaycount">0</span> times.</p>
Comments
Post a Comment