How to Trigger a Click Event using jQuery CodeUnit 28 MAY 2010

This seems a little silly to even mention here, but some people don’t now how to actually trigger events for which they’ve sculpted all that nifty jQuery code functionality for.

It’s actually really simple to be honest. You know that $(‘#button’).click(function(){ alert(‘button clicked!’); }); code you just wrote into your page?

Well try running $(‘#button’).click(); – yes that’s right, the function you previously wrote just got triggered, didn’t it?

And this applies for most events which you create actions for. Calling the event function with a parameter basically means you are creating a handle for when that event is triggered. Calling the event function without any parameters is tantamount to triggering that event.

Simple, but missed by a surprisingly large amount of people! :)

About Craig Lotter

Software developer, husband and dad to two young ladies. Writer behind An Exploring South African. I don't have time for myself any more.