tags:

views:

25138

answers:

16

Hi,

I have a JQuery dialog that requires the user to enter certain information. In this form I have a "continue" button. I would like this "continue" button to only be enabled once all the fields have content in them else it will remain disabled. I wrote a function that is called everytime a field status has changed however I don't know how to enable and disable the dialog button from this function. Any ideas?

Oops and I forgot to mention that these buttons were created as follows:

$(function() {
    $("#dialog").dialog({

    bgiframe: true,
      height: 'auto',
      width: 700,
      show: 'clip',
      hide: 'clip',
      modal: true,
      buttons: {
        'Add to request list': function() {
          $(this).dialog('close');
          $('form').submit();
        },
        'Cancel': function() {
          $(this).dialog('close');
        }
      }
    });
+14  A: 

You would want to set the disabled property

 $('#continueButton').attr("disabled", true);

Update: Ahha, I see the complexity now. The jQuery Dialog had a single line that will be of use (under the "buttons" section.

 var buttons = $('.selector').dialog('option', 'buttons');

You'll need to get the buttons collection from the dialog, loop through that to find which one you need, and then set the disabled attribute as I showed above.

Tom Ritter
Hi Tom,Sorry I was not to clear. I edited my post. Please have a look at it again to see how I created the button.
Draco
Unfortunetly the JQuery dialog doesn't make it too easy to do, so you'll have to work at it a little.
Tom Ritter
Yes, but the buttons option doesn't return DOM elements but functions - iterating through them is fine, but disabling one isn't so straightforward. Am I missing something here?
Remi Despres-Smyth
You need to work around this method to disable the buttons. Use jQuery-foo to get the resultant dom elements off the page.
Stefan Kendall
Agreed - so why is this the selected answer? -1 for incomplete.
Remi Despres-Smyth
A: 

Im trying to tackle a similar problem, but in my case. I want one of the buttons to be disabled until the user checks a checkbox.
Ive been fiddling in Firebug, attempting to get something working but with no result. For example:

>>> var button = $('button')[1]
>>> button
<button class="ui-state-default ui-corner-all" type="button">
>>> button.attr("disabled", true)
TypeError: button.attr is not a function

Any suggestions?

+1  A: 

I found a work around that may apply to people trying to do something similar. Instead of disabling the button i put a simple if statement in the function to check if the checkbox was checked. If it wasnt if displayed a simple message saying the box had to be checked before submission.

For example:

  $("#confirmation-dialog").dialog({
    modal: true,
    autoOpen: false,
    width: 600,
    overlay: {
      backgroundColor: '#000',
      opacity: 0.5
    },
    close: function() {
      $('input[type="submit"]')
      .val('Record Reading')
      .attr('disabled', false);
    },
    buttons: {
      'Confirm Reading': function() {
        if($('#check-box').attr("checked")){
          $(this).dialog('close')
          $('form')
          .addClass('confirmed')
          .submit();
        }else{
          $('#please-check').show("slide");
        }
      }
    }
  });

Anyways, hope that helps someone.

+5  A: 

This disables a button:

var firstButton=$('.ui-dialog-buttonpane button:first');
firstButton.addClass('ui-state-disabled');

You have to add the dialog id if you have several dialogs on a page.

RainerB
This worked for me - thanks RainerB
Simon Sanderson
Thanks RainerB!
Mark
This will not work for someone with js disabled.
Antony Carthy
@anthony neither will the entire dialog.. so who cares?
Josh Smeaton
+1  A: 

From a usability perspective, it's usually better to leave the button enabled but show an error message if someone tries to submit an incomplete form. It drives me nuts when the button I want to click is disabled and there is no clue to why.

erikkallen
+1  A: 

Just for the records... this post helped me to solve my problem. In short words, you have to set the disabled attribute to disabled, not to false: _send_button.attr('disabled','disabled');

This is how all the code, I also added some styles to make it look disabled:

var _send_button = $('.ui-dialog-buttonpane button:contains(Send)'); var original_text = _send_button.text(); _send_button.text('Please wait...'); _send_button.addClass('ui-state-disabled'); _send_button.attr('disabled','disabled'); _send_button.fadeTo(500,0.2);

+2  A: 

Here is my enableOk function for a jQuery dialog:

function enableOk(enable)
{
    var dlgFirstButton = $('.ui-dialog-buttonpane').find('button:first');

    if (enable) {
        dlgFirstButton.attr('disabled', '');
        dlgFirstButton.removeClass('ui-state-disabled');
    } else {
        dlgFirstButton.attr('disabled', 'disabled');
        dlgFirstButton.addClass('ui-state-disabled');
    }
}

The "first" button is the one furthest to the right. You both disable the button and set the dialog's disabled class, for the proper visual effect.

Remi Despres-Smyth
A: 

From a usability perspective, it's usually better to leave the button enabled but show an error message if someone tries to submit an incomplete form. It drives me nuts when the button I want to click is disabled and there is no clue to why.

@erikkallen: For adding new data this is valid, you don't know what you have missed. For a form updating existing data it isn't valid, a disabled submit button shows you that you have not changed anything yet.

Steve-o
+6  A: 

I wanted to be able to find the button by name (since I have several buttons in my jQuery UI dialog). I also have several dialogs on the page so I need a way to get the buttons of a specific dialog. Here is my function:

function getDialogButton( dialog_selector, button_name )
{
  var buttons = $( dialog_selector + ' .ui-dialog-buttonpane button' );
  for ( var i = 0; i < buttons.length; ++i )
  {
     var jButton = $( buttons[i] );
     if ( jButton.text() == button_name )
     {
         return jButton;
     }
  }

  return null;
}

// create the dialog
$('#my_dialog').dialog( dialogClass : 'dialog1',
                        buttons: {
                                   Cancel: function() { $(this).dialog('close'); },
                                   Submit: function() { ... } 
                             } );

// now programmatically get the submit button and disable it
var button = getDialogButton( '.dialog1', 'Submit' );
if ( button )
{
  button.attr('disabled', 'disabled' ).addClass( 'ui-state-disabled' );
}
Nick
Nice solution +1
Daok
Nice solution. I'd mention in the solution, though, that the button pane is *not* a child of the dialog - the key is setting a unique dialogClass, and using it for the selector. This got me when I was looking at it.
Remi Despres-Smyth
+4  A: 

Its very simple $(":button:contains('Authenticate')").attr("disabled","disabled");

Raman
+1  A: 

If you really want to disable a button, I found that you also need to call the .unbind() method on it. Otherwise the button may still be active, and a double-click could lead to multiple form submissions. The following code works for me:

button = $(this).parent().find("button:contains('OK')");
button.unbind();
button.addClass('ui-state-disabled');
postrational
+1  A: 
<script type="text/javascript">
$(document).ready(function() {
    $('#dialog').dialog('open');
    $(function(){

        $('#dialog').dialog({
            autoOpen: true,
            width: 400,
            modal: true,
            overlay: {
                opacity: 0.8,
                background: "black"
            },
            resizable: false,
            show: 'slow',
            buttons: {
                //"OK":function() {
                //    window.location="index.php?view=list";
                //},
                "Cancel": function() {
                    $(this).dialog("close");
                    $(this).attr("class", "button");
                }
           }
        });

        var dlgFirstButton = $('.ui-dialog-buttonpane').find('button:first');
        dlgFirstButton.addClass('button');
    });
});
</script>

i think would work with all,

Sameera Thilakasiri
+2  A: 

I found an easy way to disable (or perform any other action) on a dialog button.

    var dialog_selector = "#myDialogId";

    $(dialog_selector).parent().find("button").each(function() {
        if( $(this).text() == 'Bin Comment' ) {
            $(this).attr('disabled', true);
        }
    });

You simply select the parent of your dialog and find all buttons. Then checking the Text of your button, you can identify your buttons.

Hugh Turner
A: 

$('button:eq(0)',$('#dialog_id').dialog.buttons).button('disable');

jrey.py
+1  A: 

Hi,

I created a jQuery function in order to make this task a bit easier. Probably now there is a better solution... either way, here's my 2cents. :)

Just add this to your JS file:

$.fn.dialogButtons = function(name, state){
var buttons = $(this).next('div').find('button');
if(!name)return buttons;
return buttons.each(function(){
    var text = $(this).text();
    if(text==name && state=='disabled') {$(this).attr('disabled',true).addClass('ui-state-disabled');return this;}
    if(text==name && state=='enabled') {$(this).attr('disabled',false).removeClass('ui-state-disabled');return this;}
    if(text==name){return this;}
    if(name=='disabled'){$(this).attr('disabled',true).addClass('ui-state-disabled');return buttons;}
    if(name=='enabled'){$(this).attr('disabled',false).removeClass('ui-state-disabled');return buttons;}
});};

Disable button 'Ok' on dialog with class 'dialog':

$('.dialog').dialogButtons('Ok', 'disabled');

Enable all buttons:

$('.dialog').dialogButtons('enabled');

Enable 'Close' button and change color:

$('.dialog').dialogButtons('Close', 'enabled').css('color','red');

Hope this helps :)

sergiodlopes
A: 

Hey Chris,

In jQuery world, If you wanna select object from set of DOM element you should use eq()

Examples:

var button = $('button').eq(1);

or

var button = $('button:eq(1)');

Hope you still around here,

Goodluck :)

diewland