ayushgp ayushgp - 10 months ago 42
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

and I'm doing the following:


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

Answer Source

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));