Jump to content

addEventListener


Matej

Recommended Posts

Hello gentlemen.

 

Im trying to understand addEventListener and i came to this script

 

<p id="lol" > hello world </p>

 

var g=document.getElementById("lol");g.addEventListener("click",myfunction(){alert(event.target);},false); 

 

but it does not work , i tried it with "true"(altought im not quite suire if i understand when to use false /true).

Link to comment
Share on other sites

The problem is here: myfunction(){alert(event.target);}

 

To declare an anonymous function you use the function keyword. Simply function(){alert(event.target);}

Link to comment
Share on other sites

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"/><title>title</title><style>#lol{cursor:pointer;color:purple;}</style><script>window.onerror = function(m,u,n){alert('Javascript Error: '+m+'nURL: '+u+'nLine Number: '+n)}</script><script>window.onload = function(){document.getElementById("lol").addEventListener("click",function(e){alert('id: '+ e.target.id +'nelement: '+e.target.tagName +'ninnerHTML: '+e.target.innerHTML);},false);}</script></head><body><p id="lol">hello world</p></body></html>
Link to comment
Share on other sites

I don't seem to be able to replace...

window.onerror = function(m,u,n){alert('Javascript Error: '+m+'nURL: '+u+'nLine Number: '+n)}

...with...

window.addEventListener("error",function(m,u,n){alert('Javascript Error: '+m+'nURL: '+u+'nLine Number: '+n)},false);
Link to comment
Share on other sites

Are the error arguments standard?

 

Under the W3C model event handlers should take just one parameter that holds data about the event. This possibly also applies to the error event.

Link to comment
Share on other sites

thanks , is there any difference between

 

function(e){alert(e.target);}

and

 

function(){alert(event.target);}

there is written the second case in documentation , but it does not work , but the first does.

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...