Alex Rivera | Logout

Manually Calling an event function using JQuery

Asked 2011-08-05T16:32:39.657
12

I want to know if their is an option to call a function that is already assigned to a JQuery event.

For example:

$("#somediv").change(function() { //do something });

I want to be able to manually call that function that was originally assigned to the "somediv" element.

Thanks.

Edit
Report

3 Answers

30

Use trigger:

$("#somediv").trigger("change");
answered 2011-08-05T16:35:10.570
4

JQuery can trigger a given event; just call $("#somediv").change().

answered 2011-08-05T16:34:16.453
1

If you don't want to actualy trigger the change event (as shown in the previous answers above), you should probably extract that function into something that you can call from multiple places. It looks like you have found a place where reuse is in your application and extracting it may be the best answer.

answered 2011-08-05T16:37:11.083

Your Answer