Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI Dialog buttons

when creating a dialog with buttons like:

buttons:    {
            'button text': function(){                              
                // do something
            },

do I have access to the button within the click event handler?

$(this)

is the context/jQuery object of the whole dialog.

I doubt I have to be such creative as

$(this).find('button').attr(...)

to disabled a button there ?

like image 728
jAndy Avatar asked May 06 '10 09:05

jAndy


People also ask

How do I add a button to a dialog box?

If you're adding buttons by specifying them in the HTML itself, then use $("#dialog"). find('button') . All this is after you've called . dialog() on your selector, of course.

What is jquery ui dialog?

The jQuery UI dialog method is used to create a basic dialog window which is positioned into the viewport and protected from page content. It has a title bar and a content area, and can be moved, resized and closed with the 'x' icon by default.

What are dialog buttons?

Dialog controls are modal UI overlays that provide contextual app information. They block interactions with the app window until being explicitly dismissed. They often request some kind of action from the user.


2 Answers

The documentation for dialog() says:

The property key is the text of the button. The value is the callback function for when the button is clicked. The context of the callback is the dialog element; if you need access to the button, it is available as the target of the event object.

$('#myDialog').dialog({
    'title': 'My Dialog Header',
    'buttons': {
        'My Button': function(event) {
            // here is the modification of the button
            // opacity set to 25%, all events unbound
            $(event.target).css({opacity: 0.25}).unbind();
        }
    }
});
like image 199
artlung Avatar answered Oct 25 '22 04:10

artlung


The format of the buttons in the dialog is a <button> with a <span> inside, like this:

<button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only">
  <span class="ui-button-text">Button text</span>
</button>

So when you click, the actual click event happens on that <span> or <button>, depending on your styling (margin on the span for example), so to get the <button> just make your handler go up to the button even if you're already on it, like this:

buttons: {
  'button text': function(e){
     $(e.target).closest("button") //this is the button, do something with it :)
  }
}

Here's a quick demo of this working

like image 33
Nick Craver Avatar answered Oct 25 '22 04:10

Nick Craver