Jump to content

addeventlistener


etsted

Recommended Posts

This is my script:

 

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function addEventHandling(){
var btn1 = document.getElementById('btn1');
btn1.addEventListener('click', function(){btnClick("eggs","beef")}, true);
btn2.addEventListener('click', function(){btnClick("grid","hest")}, false);
function btnClick(arg1,arg2){
alert("This button click has "+arg1+" and "+arg2+" associated with it.");
}
}
window.onload = addEventHandling;
</script>
</head>
<body>
<button id="btn1">I have food associated with me</button>
<button id="btn2">I have food associated with me</button>
</body>
</html>
and as i understod it, when i click on btn1, btn2 should trigger, but thats not the case. I have tried to use both false and true values as the third argument, but nothing special happens. They still run the same.
Link to comment
Share on other sites

Why do you think that's the case? Where are you telling it to run the handler for btn2 when you click on btn1?

because they have the same first parameter "click".

thescientist, i have updated the script, so that btn2 has a referance

Link to comment
Share on other sites

because they have the same first parameter "click".

That is the event name, you are attaching a click event. Attaching click events to multiple elements on the page does not cause the click handlers for all of those elements to fire when you click on any element.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...