3

I am a attempting to run the below jQuery .on() method with the parameter value of 10 however whenever I do I get an

Uncaught TypeError

 $(document).on("click", '.topic-btn', displayGIF(10));

It's my understanding that how it is written runs the function without a click event causing the error.

Is there a way around this? Ultimately, I want the .on("click"... for a parameter value of 10 and a .on("dblcick"... for a parameter value of 20.

JimHawkins
  • 4,843
  • 8
  • 35
  • 55
A. Andersen
  • 149
  • 2
  • 3
  • 10
  • 5
    `displayGIF()` is called immediately. Use `, function() { displayGIF(10) }` syntax. – Tushar Oct 16 '18 at 05:10
  • $(document).on("click", ".topic-btn", function(){ displayGIF(10); });or else $(".topic-btn").on("click", function(){ displayGIF(10); }) – Suresh Oct 16 '18 at 05:13

2 Answers2

6

You can change the code to this:

$(document).on("click", '.topic-btn', {'param': 10}, function(event){
     displayGIF(event.data.param);
});

The data to on can be passed using the above method. Refer docs: http://api.jquery.com/on/

dRoyson
  • 1,466
  • 3
  • 14
  • 25
1

Do you want to invoke function displayGIF with parameter 10 or 20 based on click on dbclick. If that is the case then you can write

  $(".topic-btn").on("click", function(){
                                        displayGIF(10);
                                        });
Aamol
  • 1,149
  • 1
  • 15
  • 23