56

I found this answer on another thread..

How to add multiple buttons in Jquery UI dialog box?

Using this syntax, how do you add class to a particular button?

 $("#mydialog").dialog({
      buttons: {
        'Confirm': function() {
           //do something
           $(this).dialog('close');
        },
        'Cancel': function() {
           $(this).dialog('close');
        }
      }
    });
3
  • 15
    ...Laphroiag Whisky and a Cuban cigar. Commented Jul 15, 2011 at 3:41
  • 1
    @rockerest: You just made my day. Laphroiag 10 year single malt is my go to Scotch. Commented Nov 18, 2013 at 1:37
  • @bpeterson76 answered this as concisely as possible. i18n folks know what to do to make the solution work for them. Commented Feb 24, 2021 at 22:09

6 Answers 6

90

You Can add class to the button's in Dialog...by

$('#mydialog').dialog({
  buttons:{
    "send":{
      text:'Send',
      'class':'save'
    },
    "cancel":{
      text:'Cancel',
      'class':'cancel'
    }
  });

I think this will work for you. and you can find more answers here.

Sign up to request clarification or add additional context in comments.

7 Comments

You should change the word "className" for "class"
Although this works, it generates errors in many IDEs (like Netbeans), leading me to choose the above method(finding the buttons and adding the class) instead.
Is the errors indicating that class is reserved? You can wrap the word with quotes to avoid these errors if so.
I think it is the right approach but the class is added first to other jqueryui classes so the css rule would need !important
I think this should be marked as answer as it's the best solution.
|
53

It doesn't look like there's a great way to do this via the API, however you could add the classes in an event handler for create:

$("#dialog").dialog({
    buttons: {
        'Confirm': function() {
            //do something
            $(this).dialog('close');
        },
        'Cancel': function() {
            $(this).dialog('close');
        }
    },
    create:function () {
        $(this).closest(".ui-dialog")
            .find(".ui-button:first") // the first button
            .addClass("custom");
    }
});

If you wanted the second button, you could use:

$(this).closest(".ui-dialog").find(".ui-button").eq(1).addClass("custom") // The second button

The key is the $(this).closest(".ui-dialog").find(".ui-button"), which will select the buttons in your dialog. After that, you could apply any selector you want (including :contains(...) which might be useful if you want to select a button based on its text instead of its order).

Here's an example: http://jsfiddle.net/jjdtG/

Also note that the CSS selector for the style(s) you want to apply has to be more specific than jQueryUI's built-in selectors in order for the styling to be applied.

5 Comments

+1 This works great, but I used the open instead of create because I am opening and closing the same dialog a few times.
.dialog({dialogClass: "custom", ...}) is how it says to do it in the docs.
@popnoodles: This is a pretty old answer. That probably is the preferred way to do it nowadays.
@AndrewWhitaker though still probably preferred if you want to just add the .ui-priority-primary class to one button. It'd be nice if the buttons just supported something like a buttonClass option you could pass in with the definition.
I think this need a correction, the first button,eq(0), is the X button used to close the dialog, so index = 1 is the second button.
49

Hope it will help !

$("#mydialog").dialog({
          buttons: {
            'Confirm': function() {
               //do something
               $(this).dialog('close');
            },
            "Cancel": {
                    click: function () {
                        $(this).dialog("close");
                    },
                    text: 'Cancel',
                    class: 'custom-class'
                }
          }
        });

2 Comments

upvoted for showing example of click, text, & class together.
This is helpful. Although I wish jquery would append the class, not inserted it first.
8

Use the open event handler:

open: function(event) {
     $('.ui-dialog-buttonpane').find('button:contains("Cancel")').addClass('cancelButton');
 }

Clean, simple, piece of cake!

3 Comments

Don't forget about i18n! This approach won't work if the website supports multiple languages
It could respect i18n if you replaced the string of the button with the value for the language. You could also change your selector if need be. i18n wasn't part of the OP's question.....and for most developers, it's largely an edge case.
@bpeterson76: Maybe most developers at Oracle, but outside of the US, i18n is a main concern... usually for US users who can't cope with sites that don't use their 'special' date format.
2

Thanks to LintonB, you can add all of the property attached to a button like style, id, etc...

dialog_options.buttons = {
  'Modify': {
    click: function() {

      $(this).dialog('close');

      if (inputs.phone !== '') {
        inputs.phone.focus();
        inputs.phone[0].value = "";
      }
    },
    class: 'btn btn-labeled btn-warning',
    style: 'margin-right: 30px;',
    id: 'modificationId'
  },
  'Keep': {
    click: function() {
      $(this).dialog('close');

      _this.validatePhone(i);

    },
    class: 'btn btn-labeled btn-warning',
    id: 'conserverId'
  }
};

Comments

0

I couldn't get some of the others' answers to compile, but I instead had to use change my buttons property to be an array:

buttons: [
    {
        text: "Okay",
        class: "my-button-class",
        click: function() {
            //
        }
    },
    {
        text: "Cancel",
        click: function() {
            //
        }
    }
]

Comments

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.