1

I am trying to pass a function Which has 2 arguments to the addEventListener method on specific button but the problem is function executing directly when page loading instead of executing it when some one clicked the button.

I know how to make it work with anonymous function but I need it to work with a specific defined function instead.

function changeContent(elem,str){
    elem.textContent = str;
}

var btn = document.querySelector("button");

var p = document.querySelector("p");

var content = "Someone Clicked the Button!";

// add event listener on click to the button 

// version 1 with anonymous function worked as expected

btn.addEventListener("click",function(){
    p.textContent = content;
});

// version 2 with specific defined function not working as expected

 btn.addEventListener("click",changeContent(p,content));

any idea of the reason why second version isn't working and how to make it work as expected.

Update:

my intended and important question:

is there any way to make changeContent(p,content) function work without wrap it with a (anonymous) function in addEventListener method ?

1 Answer 1

3

Wrap it also in an anonymous function

btn.addEventListener("click",function() { 
    changeContent(p, content)
});

any idea of the reason why second version isn't working

Because changeContent is intermediate called because you're passing arguments to it (with the round brackets). In general you could pass a function to a function in two ways:

  1. Only the name, e.g. passFunction(changeContent);
  2. Wrap in a (anonymous) function, as shown above passFunction(function() { changeContent( ...);} );. The anonymous function isn't called intermediately, because you aren't passing parameters to it.

can you explain this point changeContent is intermediate called

If you pass arguments to a function, it's called. If you wrap it in an (anonymous) function, without passing arguments to the (anonymous) function, the (anonymous) function isn't called and hence also not the wrapped function.

is there any way to make it work without wrap it with a (anonymous) function

Use the .bind function. The first argument is the value of this. So you need 3 parameters for this case:

btn.addEventListener("click",changeContent.bind(this, p, content));

For more info, see the docs of .bind

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

9 Comments

can you explain this point changeContent is intermediate called
I understand that you can't pass function to another function if it called like that functionName(functionName(argument1,argument2)) is it right?
it should be like that without the round brackets functionName(functionName)
yes indeed. If you pass arguments (round brackets) to a function, it's called. Updated the answer.
indeed. And because we aren't passing the round brackets to the (anonymous) function, the (anonymous) function isn't called intermediately
|

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.