22

I want to add css class on buttons of a jquery dialog box.

Here is my code :

$(document).ready(function(){
      $('#messageBox p').html('bla bla bla. Ok?'); 
      $('#messageBox').dialog({
        modal : true,
        buttons: {
          'Yes': function() {
            doSomething();
            $(this).dialog('close');
          }, 
          'No': function() {
            doAnotherThing();
            $(this).dialog('close');
          }
        }
      });
    });

For example, I would like add ".red" class on my "yes" button.

How can I do that?

Thank you!

7 Answers 7

54
buttons: [
            {
               text: "Submit",
               "class": 'submit_class_name',
               click: function() {                     
                  $(this).dialog("close"); 
               }
            },
            {
               text: "Cancel",
               click: function() { 
                  $(this).dialog("close"); 
               }
            }
          ]
Sign up to request clarification or add additional context in comments.

2 Comments

This is the best answer! Note: use quotes around the class attribute, on iPad it throws an error (probably 'cause class is a reserved (key)word)
Same error in IE8 too. Solved with "class" instead of class
8

I've got the solution, thanks to Rich :

$(document).ready(function(){
      $('#messageBox p').html('bla bla bla. Ok?'); 
      $('#messageBox').dialog({
        modal : true,
        dialogClass: 'dialogButtons',
        buttons: {
          'Yes': function() {
                doSomething();
                $(this).dialog('close');
          }, 
          'No': function() {
                doAnotherThing();
                $(this).dialog('close');
          }
        }
      });
    });
$("div.dialogButtons div button:nth-child(1)").addClass("oneCssClass");
$("div.dialogButtons div button:nth-child(2)").addClass("anotherCssClass");

Solved!

Comments

5

There's a dialogClass option of the dialog function you can use to specify a css class for the dialog itself. You can give it a unique class name and use this class name to get a reference to any child elements of the dialog. Then, use the selectors to either get a reference to the child buttons by position or by the text it contains (probably more efficient to use the former).

Comments

1

Have you tried the addClass function?

2 Comments

But where to add that addclass function?
same answer like phoenix... Where can I put the addClass function, because buttons are made by jquery dialog and buttons don't have ID's.
1

Had same problem. Very similar to nico's solution but add the styling to the open function on the dialog:

open: function () {
                // add style to buttons (can't seem to do this via the button definition without breaking IE)
                $(".someDialog .ui-dialog-buttonset button:first").not(".buttonPrimary").addClass("buttonPrimary");
                $(".someDialog .ui-dialog-buttonset button:last").not(".buttonSecondary").addClass("buttonSecondary");
                $("#someDialog").focus();
            }

Comments

0

I had a similar situation where i needed to add some styling to a button on the fly before the dialog was shown. Getting to the button was not very simple so i figured out a very simple way of getting to the buttons. During the creation of the dialog buttons i just gave them an 'id' and then i could get a reference to them with the usual jquery selector... $("#btnDelete").addClass("disabled")

buttons: [{
        id: "btnDelete",
        text: "Delete",
        click: function () {
            Delete();
            $(this).dialog('close');
        }
    }, {
        id: "btnSave",
        text: "Save",
        click: function () {
            Save();
            $(this).dialog('close');
        }
    }, {
        id: "btnCancel",
        text: "Cancel",
        click: function () {
            $(this).dialog("close");
        }
    }]

Comments

-4

.addClass function  

1 Comment

yeah, i know this function... but I don't know how to use it in my case.

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.