I'm trying to track the clicks on a button on my website. I've tried adding the following but to no success. I'm a noob to JS..
function trackButton(e) {
onPage.innerHTML = ++i;
}
var i = 0;
var onPage = document.getElementById(‘track’);
var clickCount = document.getElementById(‘bttn’);
clickCount.addEventListener('click', function (e) {
addOne(e);
}, false);
You did some mistake:
And some tips for you:
Do these change and it must work !
UPDATE:
document.getElementById('track')
to document.getElementById('track')
I was checkin your code and its almost all right, i think that the problem its in your addOne function, here is a way to resolve the problem. i creat the button and paragraph elements in html and in javascript a variable n where we are going to storage the clicks tha the user did and we increment n when the function its called in the button's event
var n = 0; var button = document.getElementById('track'); button.addEventListener('click', trakeo); var texto = document.getElementById('number'); function trakeo(){ n++ texto.innerHTML = n; }
<,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>A Great Demo on CodePen</title> </head> <body> <button id="track">Button</button> <p id="number"></p> </body> </html>
Try to use addEventListener('click',) https://www.w3schools.com/jsref/met_element_addeventlistener.asp
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.