etsted Posted April 3, 2014 Share Posted April 3, 2014 i have noticed this functions Third parameter, useCapture, and i dont understand it. Can someone explain what it actually does? Give examples. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 3, 2014 Share Posted April 3, 2014 Here's the documentation:https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.addEventListener Link to comment Share on other sites More sharing options...
etsted Posted April 4, 2014 Author Share Posted April 4, 2014 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 More sharing options...
dsonesuk Posted April 4, 2014 Share Posted April 4, 2014 You do realise addEventListener is browser dependant attachEvent for IE (maybe not newer versions) addEventListener for other better browsers. Just in case. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 4, 2014 Share Posted April 4, 2014 and as i understod it, when i click on btn1, btn2 should triggerWhy do you think that's the case? Where are you telling it to run the handler for btn2 when you click on btn1? Link to comment Share on other sites More sharing options...
thescientist Posted April 4, 2014 Share Posted April 4, 2014 also, where are you getting a reference to btn2 in that code? Link to comment Share on other sites More sharing options...
etsted Posted April 5, 2014 Author Share Posted April 5, 2014 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 More sharing options...
justsomeguy Posted April 7, 2014 Share Posted April 7, 2014 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now