Passing parameters to click event using trigger

I have a simple jQuery onClick event bound to a link.  On the load of the page, I fetch existing data from a cookie and then call the onClick manually to setup the page (for returning visitors).  There is some animation associated with the click – but when the page loads and all of the clicks are registered, the animations look odd and out of sync (and just logically dont make sense in that situation).  So I wanted to be able to pass a parameter to the onClick letting it know if it was being called from page load or from an actual user clicking (the user clicking will cause the parameter to be null, but from page load will have data).  This is how to accomplish this:

//bind the click event, notice the 2nd parameter "from"
$('input[type=radio]').click(function (e, from) {
	//do work, make sure from isn't null/undefined
}

//using trigger later...
//i.e. in a GetDataFromCookie() function - note the 2nd parameter is true and will be passed to "from" above.
//when the trigger is called from a user actually clicking the radio button, the "from" parameter above will be null/undefined.
$('#radioButton').trigger('click', true);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s