user1032531
user1032531

Reputation: 26281

How to hide dialog buttons when a jQuery-ui dialog is opened

When opening a jQuery-UI dialog, how can I hide a button (for instance, hide the "Save" button)?

http://jsfiddle.net/ba6jwh54/1/

<!-- head --> 
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/ui-lightness/jquery-ui.css" type="text/css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.js" type="text/javascript"></script>

<!-- body -->
<div id="dialog" class="dialog" title="My Title"></div>
<a href="#" id="open">open</a>
// javascript
$(document).ready(function() {
    $('#open').click(function() {
        $("#dialog").dialog("open");
    });
    $("#dialog").dialog({
        autoOpen: false,
        height: 400,
        width: 350,
        modal: true,
        open: function() {
            var dialog = $(this);
            console.log('dialog', dialog);
            var buttons = dialog.dialog("option", "buttons");
            console.log('buttons', buttons);
            //Change names this way...
            buttons[0].text = 'Save2';
            buttons[1].text = 'Cancel2';
            dialog.dialog("option", "buttons", buttons);
            //How do I hide a button (i.e. hide Save button)?
        },
        buttons: [{
            text: 'SAVE',
            click: function() {
                alert('save');
                $(this).dialog("close");
            }
        }, {
            text: 'CANCEL',
            click: function() {
                $(this).dialog("close");
            }
        }]
    });
});

Upvotes: 2

Views: 3605

Answers (2)

Salman Arshad
Salman Arshad

Reputation: 272006

The easiest* way is to get a hold of the current dialog's widget element and .find() the button inside it:

open: function () {
    var $widget = $(this).dialog("widget");
    $widget.find(".ui-dialog-buttonpane button:first").hide();
}

Updated Fiddle

Easier than finding all the button elements on the page and guessing which one is which.

Upvotes: 3

jroot
jroot

Reputation: 320

I just added an id to the button, and updated the click function to hide it.

$(document).ready(function () {

    $('#open').click(function () {
        $("#dialog").dialog("open");
        $("#save").hide();
    });

    $("#dialog").dialog({
        autoOpen: false,
        height: 400,
        width: 350,
        modal: true,
        open: function () {
            var dialog = $(this);
            console.log('dialog', dialog);
            var buttons = dialog.dialog("option", "buttons");
            console.log('buttons', buttons);
            //Change names this way...
            buttons[0].text = 'Save2';
            buttons[1].text = 'Cancel2';
            dialog.dialog("option", "buttons", buttons)
            //How do I hide a button (i.e. hide Save button)?
        },
        buttons: [{
            text: 'SAVE',
            id: "save",
            click: function () {
                alert('save');
                $(this).dialog("close");
            }
        }, {
            text: 'CANCEL',            
            click: function () {
                $(this).dialog("close");
            }
        }]
    });

});

http://jsfiddle.net/ba6jwh54/2/

Upvotes: 1

Related Questions