views:

43

answers:

2

I'm having an issue with some of my code executing in an unexpected order. I have two buttons each with a function bound to the click event. One button is 'cancel' and it makes some display changes. The second button is 'done' which saves changes by writing them to a hidden input field and then triggers the event handler on 'cancel'. This is what my code looks like.

/*done link*/
$("#donelink").bind('click', function() { 
    var idvalues = $.map($(".ledgerline input:checkbox:checked"), function(el, i) {
  return $(el).val().toString();
}).join(',');
    $("#my_input").val(idvalues);
    $("#cancellink").triggerHandler('click');
  });

/*cancel link*/
$("#cancellink").bind('click', function() {

  /*some basic css and other property changes*/

    $(".ledgerline input:checkbox:checked").parent().parent().trigger('click');

  });

/* The function bound to this event: 
$(".ledgerline input:checkbox:checked").parent().parent().trigger('click');
*/
function { 
    /* other stuff */
    $(this).find("input:checkbox").remove();
}

What I expect to happen is that when I click on the 'done' link it will find values from all the checkboxes and write them to #my_input. And then it will hit the cancel link, removing all the checkboxes. What instead happens is that all the checkboxes are first removed, and then an empty string is written in as the value.

Any help? I don't understand why this is happening.

A: 
Russ Cam
http://jsbin.com/ufepu/editHere's a pastebin of what the code roughly looks like. How could I introduce a callback to wait for the map function? It doesn't accept another callback argument.
albertsun
thanks - what element is `$(".ledgerline input:checkbox:checked").parent().parent()`? is the markup on jsbin the same as what you have in your page?
Russ Cam
That element is the <tr> element. It has a .toggle() event handler on it.The markup in jsbin is not the same. It's a simplified version of the part of the code that's not working. I updated the pastebin too.
albertsun
+1  A: 

I've resolved this. Turns out the problem was that the event handler was being triggered twice by code elsewhere. The code I pasted above actually works fine. smacks self

Thanks for helping anyways though.

albertsun