> I'm trying to pass a function an array of buttons and function
> pointers. What I want to do is to assign functions to the arbitrary
> list of buttons. At the moment, I'm using:
>
>                                 if (button_text !== undefined){
>                                         for (var vars in button_text){
>                                                 var button = $('<input 
> type="button" value="' +
> button_text[vars] + '">').appendTo('#content');
>                                                 button.data('idnum', vars);
>                                                 
> console.log(button.data('idnum'));
>                                                 button.bind('click', 
> function(e){ return_functions[$
> (this).data('idnum')](); });
>                                         }
>                                 }
>
> and the reason I'm assigning data is because doing:
> button.bind('click',function(e){ return_functions[vars]; })
>
> always assigns the last function to all the buttons (presumably
> because vars is iterated all the way through before the event is
> called).
>
> Is there a more elegant solution to this?


Instead of using a 'for' loop you can use an 'each' iteration, then
your original idea will work fine.

http://docs.jquery.com/Core/each

if (button_text !== undefined) {
    $.each(button_text, function() {
        var vars = this;
        ...
    });
}

Reply via email to