Jez D
Jez D

Reputation: 1489

Jquery: How to attach an event handler to a different element and event

I have an indepth complicated set of jquery code, which is triggered with

$(basketUpdateTrigger).click(function() {...

What I need to do is trigger the same set of code when $('select').change happens - that is, when the selected option of a dropdoan box is changed.

I looked at .bind() but that just does not seem to answer the problem.

Upvotes: 0

Views: 34

Answers (2)

Dibu
Dibu

Reputation: 891

u can use bind method to attach an eventhandler



    $('#btn').bind('click', function()
      {
            // TO DO
      }
    );

Upvotes: 0

Salman Arshad
Salman Arshad

Reputation: 272046

You can manually invoke the click handler when the select element changes:

$("select").on("change", function() {
    $(basketUpdateTrigger).trigger("click");
});

Alternately, you can wrap the logic inside a named function; then assign that function to click and change event handlers.

Upvotes: 3

Related Questions