31

How can I position buttons from jQuery UI separately from each other. Buttons are aligned in same direction. I would like one button to be aligned to left while the other to the right. Is it possible?

turezky
  • 856
  • 4
  • 11
  • 16

13 Answers13

30

OK, looking at this through Firebug...

The Dialog control create a div with a class called ui-dialog-buttonpane, so you can search for that.

If you are dealing with multiple buttons, you will probably want :first and :last attributes.

So, $(".ui-dialog-buttonpane button:first) should get you the first button. and $(".ui-dialog-buttonpane button:last) should get you the last button.

From there you can modify the css/style to put each button on the right and left (modify the float values).

Anyway, that is how I would approach it right now.

alex
  • 479,566
  • 201
  • 878
  • 984
Chris Brandsma
  • 11,666
  • 5
  • 47
  • 58
  • Then expand the selectors a bit. $("div.dialog < .ui-dialog-buttonpane button:last) and $(""div.dialog < .ui-dialog-buttonpane button:first) should work. The div.dialog should be the owner of the dialog in the first place. – Chris Brandsma Jun 27 '09 at 16:07
  • I'm seeing the same thing as Brian C. Lane. I have multiple dialogs in the page and Chris' suggestion works fine on the first one, but any dialogs opened after that don't align correctly. I can't figure out why. I call the selection code in the "open" event of the dialog, so I would think it should work... weird. – Nathan Beach Oct 03 '12 at 21:05
  • Ah, now I get it. When you do the buttonpane selector it's collecting all the buttons in the whole page, so it's still only doing the first one ever created by jQuery. I'm now doing something like this and it works (narrowing down the scope of the button set): var form = $("#" + dialogId); var buttonpane = $(form).nextAll(".ui-dialog-buttonpane"); var theButton = buttonpane.find("button:first"); theButton.css("float", "left");` – Nathan Beach Oct 03 '12 at 21:45
  • i guess it's not possible to do line breaks in comments -- sorry that looks like crap... – Nathan Beach Oct 03 '12 at 21:48
  • 1
    All the above is precisely why I spin my own "button pane". Rummaging around the DOM and fiddling around with CSS is error prone. I put a "BODY" div and "BUTTON" div in my dialogs and cleanly handle the display in a sleek DOM/CSS way. – horace Dec 23 '22 at 22:12
18

This is the way that I was able to accomplish the results.

 $('#dialog-UserDetail').dialog({
            autoOpen: false,
            height: 318,
            width: 531,
            modal: true,
            resize: false,
            buttons: {
                DelUser:{ 
                    class: 'leftButton',
                    text: 'Delete User',
                    click : function (){
                        alert('delete here');
                    }
                },
                "Update User": function () {
                       alert('update here');
           },
                Close: function () {
                    $(this).dialog("close");
                }
            }
        });

Then in Styles add the !important flag, which is needed because the class comes first and is overwritten by jquery.

<style>
    .leftButton
    {
        margin-right: 170px !important;
    }
</style>
Brian Mains
  • 50,520
  • 35
  • 148
  • 257
The Cook
  • 189
  • 1
  • 2
  • 3
    If the example above does not work with your version of jQuery, there is a similar example (answered by Raphael Schweikert) on a different Stackoverflow question: [Apply CSS to jQuery Dialog Buttons](http://stackoverflow.com/questions/1828010/apply-css-to-jquery-dialog-buttons) – shawad Jul 25 '13 at 16:28
13

I wound up with the following solution (based on the answer by The Cook). This has a number of advantages (for me) over the other answers:

  • Pure HTML/CSS - no javascript
  • No need to set a fixed width on the dialog (button will always align to the left edge, even if the user resizes the dialog)
  • No need for the !important flag in the CSS

The HTML I used (Slightly modified from the answer by The Cook):

$('#dialog-UserDetail').dialog({
        autoOpen: false,
        width: 'auto',
        modal: true,
        buttons: {
            DelUser:{ 
                class: 'leftButton',
                text: 'Delete User',
                click : function (){
                    alert('delete here');
                }
            },
            "Update User": function () {
                   alert('update here');
       },
            Close: function () {
                $(this).dialog("close");
            }
        }
    });

Then I used the following CSS:

.leftButton
{
    position: absolute;
    left:8px;
}

With the result that the "leftButton" button is always fixed at 8px off the left edge of the dialog, while the other buttons are right justified. If you have more than one button you need left justified, then you would need to increase the left parameter by the width of the previous buttons plus whatever spacing you want for each button, which is less than elegant in my opinion. However, for a single left-justified button this works like a charm.

ibrewster
  • 3,482
  • 5
  • 42
  • 54
4

You'd first have to change width of .ui-dialog-buttonset to 100% in jquery-ui.css

.ui-dialog .ui-dialog-buttonpane .ui-dialog-buttonset { width: 100%;}

Then in your modal declaration, for buttons, you could do something explicit like below:

buttons: [
        {
           text: "Close",
           open: function(){
                        $(this).css('float','left');}, 
                  }
        }
             ]
Angad
  • 3,389
  • 3
  • 30
  • 40
4

you could also add class to your dialog (http://docs.jquery.com/UI/Dialog#option-dialogClass) to localize your styling.

pashcan
  • 41
  • 1
2

Ok,As @The Cook's answer,you can change this css in style tag.Think by another way.you can add style within button,no need css anymore.Good Luck.

                      "Ok":{                                
                            style:"margin-right:640px",                                
                            click: function () {
                                //Your Code 
                            }
                        },
lwin
  • 3,784
  • 6
  • 25
  • 46
1

I found the .ui-dialog-buttonset collapses down with no width anyway so to position two bottons in the bottom left and right corner added just css like this:

.ui-dialog-buttonset {width:100%}
.ui-dialog-buttonset .ui-button:last-child {float:right !important;}

The first line make the button set container 100% wide, the second one pushes the last button found to the right.

Shaun
  • 11
  • 1
1
.ui-dialog .ui-dialog-buttonpane .ui-dialog-buttonset {
  float: none !important;
  text-align:right;
}

.ui-button-left {
  float: left;
}

This does not impact the default buttons, they will appear to the right. Also, you can place as many buttons on the left as you want without special formatting for each button. To set the class of the left buttons, there are many ways to do that.

As part of the dialog definition:

$("#mydialog").dialog(
    {buttons: [
        {"class": "ui-button-left", "text": "Delete"},
        {"text": "Cancel"},
        {"text": "Save"}
    ]});

Or you can do something like this:

$("#mydialog").dialog(
    {buttons: [
        {"id": "myDeleteButton", "text": "Delete"},
        {"text": "Cancel"},
        {"text": "Save"}
    ]});

And later style the css for the 'myDeleteButton' object as floating left. Setting the button id can be helpful if you also want to show or hide the button without recreating the dialog.

Tested in Chrome, Safari, FF, IE11, jQuery UI 1.10

spekary
  • 408
  • 5
  • 10
1

I got the same problem but I found the easy solution that we change order of buttons Ok or close of form dialog whenever we make the definition jquery ui dialog form.

nvtthang
  • 604
  • 2
  • 10
  • 27
1

After select, try:
.prependTo(".ui-dialog-buttonpane");

Falci
  • 1,823
  • 4
  • 29
  • 54
0
$('#dialog-UserDetail').dialog({
            autoOpen: false,
            height: 318,
            width: 531,
            modal: true,
            resize: false,
            buttons: {
                DelUser:{ 
                    text  : 'Delete User',
                    click : function (){
                        alert('delete here');
                    }
                },
                space: { 
                    text  : '',
                    id : 'space',
                    width : '(the distance you want!)',
                },
                "Update User": function () {
                       alert('update here');
           },
                Close: function () {
                    $(this).dialog("close");
                }
            }
        });
$("#space").visibility("hidden");
eugen
  • 8,916
  • 11
  • 57
  • 65
  • in this way, no need css, just make the middle button visibility="hidden",the button's width should be the xxpx as you like,anything you dont understand, you leave a message, i'm giles. feel free to talk – user2653803 Aug 05 '13 at 16:02
  • A hidden button just to not need any CSS... now that's a hack. :) – Oskar Berggren Sep 18 '18 at 22:17
0

I use the following approach to left align only the first button. This works for any dialog width:

    open: function(event, ui) {
        var firstButton = $(this).parent().find('.ui-dialog-buttonpane button:first')
        var firstButtonPos = firstButton.position().left - 15;
        firstButton.css({ marginRight: firstButtonPos + 'px', color: 'red' });
    },
Peter Thoeny
  • 7,379
  • 1
  • 10
  • 20
0

All I did was add the following to the CSS to center the buttons. The key here is setting the width to 100%, bc checking with firebug, the width was only size of the buttons.

.ui-dialog-buttonset {
    width: 100%; 
    text-align: center
}
İsmail Y.
  • 3,579
  • 5
  • 21
  • 29
Cenote
  • 1
  • 1