13

Is there an easy way to apply CSS/icons to the modal buttons on a jQuery UI modal dialog box?

If I include the HTML to display an icon with the button text, it shows the HTML as text rather than rendering the code.

I'm guessing I could write some jQuery to find the button and overwrite the HTML with what I want, but I'm hoping there's an easier more direct way.

4 Answers 4

19

I can see its pretty old question but you can do it now in much nicer way in jQuery UI, just add 'class' or 'style' properties to button object like this:

$("#id-dialog").dialog({
    modal: true,
    buttons: [
        { text: "Save", click: function () { alert("save"); }, class:"sampleClass1", style:"color:Red" },
        { text: "Cancel", click: function () { alert("close"); ;}, class:"sampleClass2"}
    ]
});  
Sign up to request clarification or add additional context in comments.

1 Comment

I've updated this as the accepted answer, as it's a better more modern solution.
12

This is what I'm using right now for our projects:

$("#id-dialog").dialog({
        modal: true,
        buttons: {
            'Login': logIn,
            Cancel: logOut
        },
        open: function() {
            $buttonPane = $(this).next();
            $buttonPane.find('button:first').addClass('accept').addClass('ui-priority-primary');
            $buttonPane.find('button:last').addClass('cancel').addClass('ui-priority-secondary');                        
        }
    });

First and last work in this case since there are only two buttons. You can use the :nth-child(index) if you have more than two buttons.

Another way to do this would be to refer to the parent element which encompasses both the dialog div element as well as the buttonpane div element, and then look for the individual buttons within the parent element.

3 Comments

Good thinking - I forgot about those particular selectors. It seems there's no more direct way to go about this - so I'm going with your answer as it seems a fairly clean approach
Thanks. BTW, UI 1.9 should have support for enhanced button control. And if you'd like to just patch 1.8, you can find more about the patches here.
Had to twiddle the acquisition of $buttonPane because $(this) turned out not to be right next to the buttons. $(this).parent() is the outer element for the whole dialogue box.
3

This thread - jQuery UI confirmation dialog - manipulating output - seems to provide a cleaner option, which should execute faster than a find.

$('.ui-dialog-buttonpane').children('button')[1]

I got stuck trying to use it at first, but got it working after noticing that this will not return a jquery DOM object; it returns the html, so you need to stick it inside a jquery operator to use it. For example -

var button1 = $('.ui-dialog-buttonpane').children('button')[1];
$(button1).removeClass('ui-button-text-only').addClass('ui-button-text-icon');

1 Comment

You can also use $('.ui-dialog-buttonpane').children('button').eq(1) to have it as a jquery object
1

Yes, you can overwrite modal dialog css classes to suit your needs. For example you create dialog with specifying your custom class:

$("#id-dialog").dialog({ 
            dialogClass: "loadingScreenWindow",
            ...

And then in css:

/* hide the title bar on the loading screen */ 
.loadingScreenWindow .ui-dialog-titlebar {
  display: none;
}

See http://docs.jquery.com/UI/Dialog#theming for the dialog style classed available

2 Comments

Thanks for this, although how would you suggest that this route would be used to add an icon to the first button, but not the second button contained within the 'ui-dialog-buttonpane' area?
yeah... i tried this, but damn if it doesn't use my custom classes.

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.