views:

467

answers:

2

I am relatively new to jQuery but the below code seems logical but is not working as I would expect. I am utilizing the Colorbox jQuery plugin.

My intention is to only add a listener for the 'cbox_closed' event on 'a' elements who have an id that contains 'Remove'. Unfortunately, as presently implemented this adds the listener on all raisings of the 'cbox_closed' event.

Am I missing something or is this not a valid means of adding an event listener?

$('a[id*="Remove"]').bind('cbox_closed', function() {
    var row = $($.fn.colorbox.element()).parents('tr');
    row.fadeOut(1000, function() {
        row.remove();
    });
});
A: 

Shouldn't that be

$("a[id*='Remove']").bind('cbox_closed', function() {

You seem to be missing qoutes around the Remove value

Or I misunderstood your problem

jitter
If I am not mistaken I believe he is referring to the instance when the ID's change after the bound event and the ID no longer contains remove. The bind event would still be bound to those anchors in his code.
cballou
I didn't have the quotes but unfortunately that made no difference. :(
ahsteele
+1  A: 

Try this:

$(document).bind('cbox_closed', function() {
  if ( $( $.fn.colorbox.element() ).attr('id').match('Remove') ){
   alert('Remove me!');
  }
})

The event will always trigger when the ColorBox is closed. You would have to modify the plugin itself to prevent this event from firing in specific cases. So, the above code binds to the event then looks for your specific ID. I hope my explanation helps :)

fudgey