1

I have the following problem. I'm trying to add dynamic buttons in a modal dialog. But I don´t know, how I can give the function to each button.

I have the following:

     for(i=0;i<buttons.length;i++){
          arrButton[i] = { id :  buttons[i].name  , text :  buttons[i].label , click : function(){  buttons[i].onclick  } };
     } 
     $("#divFormulario").dialog
        ({  modal    : true, 
            title    : titulo,
            resizable: false,
            buttons  :  arrButton     
        });

For example, if I have the following : buttons[i].onclick = "functionAlert();, when I create the button have the click event with buttons[i].onclick, but I need the click event with functionAlert(). What am I doing wrong?

3
  • Can't you just put click : function(){ functionAlert(); } ? Commented Apr 19, 2011 at 14:38
  • You might as well just use the same object format for your button array as it's required by the dialog, so you could just pass your "buttons" array instead of copying the whole thing into "arrButton". Commented Apr 19, 2011 at 14:45
  • If your dynamicaly creating the buttons it would be a good idea to use .live() Commented Apr 19, 2011 at 14:51

1 Answer 1

3

Assuming you have an function:

function functionAlert() { ...some code... }

Instead of passing functions as strings, just do:

buttons[i].onclick = functionAlert;

Then your loop should be:

for(i=0;i<buttons.length;i++){
   arrButton[i] = { id :  buttons[i].name  , text :  buttons[i].label , click : buttons[i].onclick };
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank but I tried this, the problem with this is the following message "Microsoft JScript: ..... 'apply'" the error is produced near of here "h.click.apply(b.element[0],arguments)" File of Jquery

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.