14

I am using jquery to disable the submit button and load loading image,

In submit button I am using the folowing :

<div id="registerbtn" name="registerbtn">
<input type="submit" class="btn btn-primary" icon="ok white" value="Register" id="register" name="register"/>
</div>
<div class="span4" style="display:none;" id="loadingtext">
                            <?php echo  $imghtml=CHtml::image('images/loading.gif');?>                      
                        </div>

and in JQuery, I am using following code :

$(document).ready(function() {

    $('#register').click(function() {
        $('input[type="submit"]').attr('disabled','disabled');

     });
        $("#loadingtext").show();
    });
});

When I do this, then this button is disabled permanently, but if I want to remove then what should I do ??

Rohitashv Singhal
  • 4,517
  • 13
  • 57
  • 105
  • can you specify when you want to enable submit button – Prashant16 Jan 05 '13 at 10:21
  • I am using yii to submit this form, when this form is submitted then it is validated, at this time i want to disable this button, if there are errors then I want to enable otherwise, proceed to next view – Rohitashv Singhal Jan 05 '13 at 10:24
  • Possible duplicates: http://stackoverflow.com/questions/1785071/jquery-remove-attribute http://stackoverflow.com/questions/1594952/jquery-disable-enable-submit-button?rq=1 – Stefan Neubert Jan 05 '13 at 10:24

5 Answers5

17

Use .prop() method and use the $(this) to reference the target element within the callback function

jQuery(function($) {

  const $register = $("#register"),
        $loading = $("#loading");

  $register.on("click", function() {
  
    $(this).prop('disabled', true);
    $loading.show();
    
    setTimeout(function() {
      $register.prop('disabled', false);
      $loading.hide();
    }, 2000);

  });

});
<input id="register" value="Register" type="submit">
<div id="loading" style="display:none;">Wait 2 sec...</div>


<script src="//code.jquery.com/jquery-3.1.0.js"></script>
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
10

This is answered in the jQuery FAQ

How do I disable/enable a form element?
There are two ways to disable/enable form elements.

Set the 'disabled' attribute to true or false:

 // Disable #x
 $('#x').attr('disabled', true);
 // Enable #x
 $('#x').attr('disabled', false);

Add or remove the 'disabled' attribute:

 // Disable #x
 $("#x").attr('disabled', 'disabled');
 // Enable #x
 $("#x").removeAttr('disabled');

Update: Now the way to do it (as said by FAQ) is only:

// Disable #x
$( "#x" ).prop( "disabled", true );

// Enable #x
$( "#x" ).prop( "disabled", false );
PhoneixS
  • 10,574
  • 6
  • 57
  • 73
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
0

To permanently remove the button

$('#register').remove();

To restore the button

$('#register').attr('disabled',false);
PassKit
  • 12,231
  • 5
  • 57
  • 75
0

Is $('#divID').remove(); what you're looking for?

http://docs.jquery.com/Manipulation/remove

Ignacio Correia
  • 3,611
  • 8
  • 39
  • 68
0

Enable disabled properties

 $('#apply-coupon').removeAttr('Disabled');

Disable disabled properties

$('#apply-coupon').attr('disabled', 'disabled' );
Mosam Prajapati
  • 189
  • 2
  • 6