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! :)

Related Posts:

About Craig Lotter

South African software architect and developer at Touchwork. Husband to a cupcake baker and father to two little girls. I don't have time for myself any more.

  • I've been struggling for this and thank you for your help.

  • Eek, yet another potentially hand-craafted spam comment that got through! O.o

  • Is there a way to trigger a click event from another element’s click event? This code assumes that the link is an actual hyperlink that may or may not have other click events tied to it. Either way, the link should redirect you to a new page.

    $(‘#myDiv’).click(function() {
    $(this).find(‘a:last’).click(function(e) {
    // Prevent from bubbling, which would cause an endless loop