Barış Velioğlu
Barış Velioğlu

Reputation: 5817

How to trigger a js function with Jquery

I want to trigger this jquery function by using trigger method of JQuery. How can I do that ? Actually I dont even know the trigger method is suitable for user defined functions.

$('a.pop').click(function() {alert('testing'); }

this is not working

 $('a').trigger(testtt);

 var testtt = function(){ alert('aaa');} 

Upvotes: 1

Views: 202

Answers (5)

gen_Eric
gen_Eric

Reputation: 227230

.trigger() is used to trigger event handlers (custom or built-in'). Since you bound your function to the "click" handler, you can use trigger like so to call it:

$('a.pop').trigger('click');

jQuery's event binding methods can also be called without parameters to trigger them, which means you can also do this:

$('a.pop').click();

Upvotes: 0

zatatatata
zatatatata

Reputation: 4821

Reading from jQuery API, the following should work.

$('a.pop').trigger('click');

Upvotes: 0

Stefan Kendall
Stefan Kendall

Reputation: 67812

$('a.pop').click(), or if you're triggering some dynamic method, or custom event:

$('a.pop').trigger(eventName), e.g: $('a.pop').trigger('click');

Upvotes: 1

stimms
stimms

Reputation: 44054

You can trigger a click event on the element by simply running

$('a.pop').click()

Upvotes: 1

Jon
Jon

Reputation: 437356

Very similar to the way you install the event handler:

$('a.pop').click();

If you have the name of the event you want to trigger as a string, you can also do it this way:

$('a.pop').trigger('click');

This is also the solution to use if you want to pass crafted data to the event handler -- trigger also accepts a second parameter.

Upvotes: 1

Related Questions