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 trialtroy beckett
12,035 PointsCan't call the function from addeventlistener.
The rest of the function works fine but for some reason when I call it using addeventlistener it doesn't work. Any ideas why?
<body> <button id="button1">Click Me</button> <script>
function whichButtonClicked(event){
var whichButton;
if(event.which){
switch(event.which){
case 1:
whichButton = "Left Button Clicked";
break;
case 2:
whichButton = "Middle Button Clicked";
break;
case 3:
whichButton = "Right Button Clicked";
break;
default:
whichButton = "Invalid Button Clicked";
break;
}
}
else{
switch(event.button){
case 1:
whichButton = "Left Button Clicked";
break;
case 2:
whichButton = "Middle Button Clicked";
break;
case 3:
whichButton = "Right Button Clicked";
break;
default:
whichButton = "Invalid Button Clicked";
break;
}
}
alert(whichButton);
}
document.oncontextmenu = preventContextMenu;
function preventContextMenu(event){
event = event || window.event;
if(event.preventDefault){
event.preventDefault();
} else {
event.returnValue = false;
}
}
document.getElementById("button1").addEventListener("click",function(){whichButtonClicked(event);});
</script>
2 Answers
Chyno Deluxe
16,936 PointsThe alert executes for me when i click the button. View CSSDeck
Do you have a pop up blocker maybe?
troy beckett
12,035 PointsIt works when I click the left mouse button but not the right.