ayushgp ayushgp - 2 months ago 5
Javascript Question

How to pass extra parameter to event handling callback?

I have a button on which I want to attach an event listener. I also need to pass a extra parameter url to this function. I read about

apply
and I'm doing the following:

$('#list-button').on('click',postListing.apply([url]));


My problem is that as soon as this script is loaded
postListing
is called. I am not calling the function anywhere else. I need it to be called only on click.

Answer

The difference between bind and call/apply is that bind doesn't call the function immediately much like it loads the data with the variable when needed

You can reformat your code so it looks like this

$('#list-button').on('click', postListing.bind(this, url));
Comments