22

I am using jQuery UI Dialog box for ajax form submit. I want change the text of my save button to wait , when user click on it and back to Save when ajax call complete. pls help me

Saokat Ali
  • 1,055
  • 2
  • 12
  • 18
  • 3
    If I could choose the best answer for this question, it would be [this one by ManojRK](http://stackoverflow.com/questions/4591642/dynamically-changing-jquery-ui-dialog-box-button-text#15863018). Well done, Manoj. – cssyphus Jul 10 '13 at 19:08

11 Answers11

28

Although the question is very old I find the answer to be very simple and it has not been given here.

  • You can set an id for the button and use it.
  • All the dialog buttons are jQuery UI buttons hence you can use $().button() function to modify the button.

The JavaScript code is:

$('#dialog').dialog({
    buttons:[{
        id: 'buttonId',
        click: function() {
            // your function
        }]
});
$('#buttonId').button('option', 'label', 'Please wait...');
ManojRK
  • 952
  • 9
  • 22
  • 5
    [See also this answer](http://stackoverflow.com/questions/10312835/set-jquery-ui-dialog-button-id#10312946) for simpler (non-array) syntax. Works great in conjunction with this answer. – cssyphus Jul 10 '13 at 19:33
23

Presuming you're using .dialog() with the buttons option, you can assign a custom class to the submit button, and then target the inner button text via the class that is assigned to the span (ui-button-text):

    $("#dialog-test").dialog({
        title: "My dialog",
        buttons:
            [
              {
                  text: "Submit",
                  click: function() {
                    $(".my-custom-button-class > .ui-button-text").text("Please Wait..."); 
                    $("#some-form").submit();
                  },
                  'class': 'my-custom-button-class'
              }
            ]
    });

When your save completes via the submit(), you could use the same call to set the text back to what you want.

Justin R.
  • 360
  • 1
  • 5
7

If it helps anyone: full implementation with example (PS: I borrowed getDialogButton() from another post on this site but can't remember link).

//-> Change to Loading Notice:
setButton('.settingsDialog', 'Save', 'Saving...', false);
setTimeout(function() { setButton('.settingsDialog', 'Saving...', 'Save', true); }, 800 );},

//Select the Dialog Buttons
function getDialogButton( dialog_selector, button_name ) {
  var buttons = $( dialog_selector + ' .ui-dialog-buttonpane button' );
  for ( var i = 0; i < buttons.length; ++i ) {
     var jButton = $( buttons[i] );
     if ( jButton.text() == button_name )
     {
         return jButton;
     }
  }
  return null;
}


//Button Controller for AJAX Loading:
function setButton(sDialogClass, sButtonName, sNewButtonName, bEnabled){
    var btn = getDialogButton(sDialogClass, sButtonName);
    btn.find('.ui-button-text').html(sNewButtonName);

    if (bEnabled) {
        btn.removeAttr('disabled', 'true');
        btn.removeClass( 'ui-state-disabled' );
    } else {
        btn.attr('disabled', 'true');
        btn.addClass( 'ui-state-disabled' );
    }
}
JaredBroad
  • 660
  • 1
  • 8
  • 17
  • 2
    this bit: btn.find('.ui-button-text').html(sNewButtonName) saved my bacon. But to get the button I found it easier to get the id of the button with a selector (you can set id:"myButtonID" in the array where you set the buttons then use a normal selector $("#myButtonId")..find('.ui-button-text').html("new text") – Gurnard Nov 10 '11 at 09:59
  • Perfect. Exactly what I was looking for. Should be built into jQueryUI dialog. – dualmon Nov 28 '12 at 09:16
4
$(".ui-dialog-buttonpane button:contains('Save') span").text("Please wait...");
Danilo Valente
  • 11,270
  • 8
  • 53
  • 67
CleanBold
  • 1,551
  • 1
  • 14
  • 37
2

Note the correct way to create multiple buttons on the fly:

'buttons', [
    {
        text: "Download",
        click: function () {...}
    },
    {
        text: "Not now",
        click: function () {...}
    }
]

Here is an example using the non-array style: See this jsFiddle for an example.

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111
1
$("#dialog-test").dialog({
    title: "My dialog",
    buttons:
        [
          {
              text: "Submit",
              click: function() {
                $(".my-custom-button-class > .ui-button-text").text("Please Wait...");
                //You may use $(this) as selector or allso $("#dialog-test")
                $(this).parent().children('.ui-dialog-buttonpane').children().children().html('Please wait..');
                //As you have only one button, it should not matter to specify child element, but you can like this:
                //$($(this).parent().children('.ui-dialog-buttonpane').children().children()[0]).html('Please wait..');

                $("#some-form").submit();
              },
              'class': 'my-custom-button-class'
          }
        ]
});
Aivar Luist
  • 1,641
  • 1
  • 10
  • 2
1

For my version of jquery ui (1.11.4), this format worked to change the button text:

$('#setActionButton').button('option').text('new text');

However, it did not reset the button with the new text into the dialog! That was frustrating. JaredBroad's answer above is the one that worked for me, first pulling all the buttons out of the dialog, and then looping through to find the button for renaming. Then it changed the text every time.

Community
  • 1
  • 1
excyberlabber
  • 629
  • 1
  • 10
  • 17
1

Use $().text('Please Wait') before you do the AJAX call and then add $().text('Save') as the last operation in the success callback.

Note that for this, you must use a button element, not an input element:

<button>Text here</button>
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
1

For those who don't wanna add extra id/class or who don't wanna repeat click callback function:

// initialization
$('#my-dialog').dialog({
    buttons: [
        {
            text: 'Submit',
            click: function () {...}
        }
    ]
});
// You can simply change button text by this way
$('#my-dialog').dialog('option', 'buttons.0.text', 'wait...');
Yan
  • 854
  • 1
  • 8
  • 15
0

I find that if I use many dialogs with the same buttons then I can define class to each button in each dialog and then change the text on all buttons.

$('#dialog1').dialog({
        buttons:[{
                    class: "btnOK",
                    click: function () {
                        ...
                    }
                },
                {
                    class: "btnClose",
                    click: function () {
                        ...
                    }
                }]
    });

$('#dialog2').dialog({
        buttons:[...]
    });    

$('.btnOK').button('option', 'label', 'Your text here');
$('.btnClose').button('option', 'label', 'Your text here');

Also instead of class you can define id for each button (however id should be unique)

Kirill Ch
  • 5,496
  • 4
  • 44
  • 65
0

You will have to use beforeSend and complete options accordingly. Check out the docs for more info:

http://api.jquery.com/jQuery.ajax/

Sarfraz
  • 377,238
  • 77
  • 533
  • 578