Stephen Woodbridge wrote:
> I have a bunch of buttons, and use the following to assign click events.
> 
>              $(".button").each( function() {
>                  var data = { check_dirty: 1 };
>                  data.action = $(this).text().toLowerCase();
>                  if (data.action == 'save') data.check_dirty = 0;
> 
>                  $(this).css("text-align", "center");
>                  $(this).bind( "click", data, check_save);
>              });
> 
> 
> The problem is that all the click handlers get the data that is assign 
> by the last button processed. If I set through firebug, they are all 
> getting assigned the correct values as the each loop is executing, but 
> data seems to be global rather then scoped to the anon function!
> 
> What am I missing here?
> 

To add to this puzzle if I replace check_save reference to a function, 
with an anonymous function it works correctly. Like this:


             $(".button").each( function() {
                 var mydata = { check_dirty: 1 };
                 mydata.action = $(this).text().toLowerCase();
                 if (mydata.action == 'save') mydata.check_dirty = 0;

                 $(this).css("text-align", "center");
                 $(this).bind( "click", mydata,
                     function (e) {
                         var check_dirty = e.data.check_dirty;

                         if (check_dirty && form_dirty) {
                             $("#continue").unbind("click");
                             $("#continue").bind("click", e.data, 
                function(e) {
                                 clear_form_data();
                                 actions[e.data.action](e.data.data);
                                 $.unblockUI();
                             });
                             $("#save_it").unbind("click");
                             $("#save_it").bind("click", function (e) {
                                 action_save();
                                 if (e.data.action &&
                                     typeof actions[e.data.action] == 
"function")
 
actions[e.data.action](e.data.data);
                                 $.unblockUI();
                             });
                             $("#cancel").unbind("click");
                             $("#cancel").bind("click", function() {
                                 $.unblockUI();
                             });
                             $.blockUI(saveMessage);
                         } else
                             if (typeof actions[e.data.action] == 
"function")
                                 actions[e.data.action](e.data.data);
                     });
             });

_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to