1

Is it possible to create a button array then append it to a jQuery dialog?

Something along these lines. Forgive my errorful code within the for loop, just not sure how to do this at all.

function setAutoDialog()
{
    var testArray = ["T1", "T2"];
    $('#autoDialog').dialog({
        autoOpen: false,
        width: 'auto',
    });
    var buttons = {};
    for(var i=0; i<testArray.length; i++){
        buttons += [testArray[ix] : Test()]
    }
    $('#autoDialog').dialog('option', 'buttons', buttons);
}

function Test()
{
    alert("worked");
}

1 Answer 1

2

Something like this should work :

function setAutoDialog(){
    var testArray = ["T1", "T2"];

    var testFunction = function () {
        alert("worked");
    }

    var myButtons = {};

    for(var i = 0; i < testArray.length; i++){
        myButtons[testArray[i]] = testFunction;
    }

    $('#autoDialog').dialog({
        autoOpen: false,
        width: 'auto',
        buttons : myButtons
    }); 
}

"For instance on click instead of alert(worked) I want to get alert(buttonClicked.val())?" It would be something like :

function setAutoDialog(){
    var testArray = ["T1", "T2"];

    var myButtons = {};

    for(var i = 0; i < testArray.length; i++){
        var testFunction = function () {
            alert(testArray[i]);
        }

        myButtons[testArray[i]] = testFunction;
    }

    $('#autoDialog').dialog({
        autoOpen: false,
        width: 'auto',
        buttons : myButtons
    }); 
}
Sign up to request clarification or add additional context in comments.

3 Comments

Perfect thanks! I thought it would work similar to that but I couldn't figure out the syntax.
How do I get the value of one of the buttons within that array? For instance on click instead of alert(worked) I want to get alert(buttonClicked.val())?
Is there a way to do this but also dynamically add a different background color to each button???

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.