Friday, February 17, 2012

Recursion in jQuery event


This event is attached to a set of elements:




var elems = $(this).filter(function(){
...
});



How can I trigger a change event on these elements within a change event handler, but ignore the current element?




elems.bind('change input', function(){

// do some stuff...

// then trigger change, but not on "this", to avoid the recursion issue
elems.change();

});



Basically I want ot ignore this from elems , but only in my "manual" change()-call above...

3 comments:

  1. var elems = $(this).filter(function(){
    ...
    });

    elems.bind('change input', function(){


    // then trigger change, but not on "this", to avoid the recursion issue
    elems.not(this).change();

    });


    Though I must say, this scenario looks like a poor design issue.
    When element get changed it sets a change event callback to some elements including him self... sound awkward and that recursion is just a symptom to other bigger problem...

    It probably can be done in a a lot cleaner way then this.

    ReplyDelete
  2. Try this:

    elems.not(this).change();

    ReplyDelete
  3. You can just filter out the current element:

    elems.not(this).trigger('change'); // or just .change()

    ReplyDelete