0

I'm trying to call a function through an eventlistener in Javascript, and within the function I have invoked another eventlistener. However on doing this, my second listener is completely ignored. Is there any condition which I need to follow to make this work?

    document.getElementById("my_canvas").addEventListener("mouseenter", this.getAttention); //first event listener 

getAttention: function(e){
    document.addEventListener("onclick", function(){ console.log("Hello World!"); });

Here the "Hello World" is not consoled.

Please help.

2 Answers 2

2

It is ignored because the first parameter expects event string to be without "on", try this

document.addEventListener("click", function(){ console.log("Hello World!"); });

Sign up to request clarification or add additional context in comments.

Comments

1

When invoking the addEventListener method you pass in the name of the event as the first argument. The name of the click event is 'click'.

document.addEventListener('click', myFunc);

The syntax for registering a click event handler as a HTML attribute is onclick

<button onclick="myFunc"></button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.