Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple window onclick event in one page

For some reason, I need two windows onclick event on one page

window.onclick = function(event) {
    alert("abc") 
}

window.onclick = function(event) {
    console.log("abc");
}

but only the second one run. Any idea how I can make both works.

like image 286
Võ Minh Avatar asked Jan 08 '18 10:01

Võ Minh


1 Answers

You are overwriting window.onclick so only the latter will run. You should use addEventListener instead if you want to use vanilla JS.

window.addEventListener('click', function(event) {
    alert("abc") 
});

window.addEventListener('click', function(event) {
    console.log("abc");
});

If you are using jquery, then you can use jQuerys .click.

$(window).click(function(event) {
    alert("abc") 
});

$(window).click(function(event) {
    console.log("abc");
});
like image 132
Jim Wright Avatar answered Sep 23 '22 09:09

Jim Wright