Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable and Enable submit button in jquery

Tags:

html

jquery

css

yii

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 ??

like image 482
Rohitashv Singhal Avatar asked Jan 05 '13 10:01

Rohitashv Singhal


People also ask

How disable submit button until form is filled jQuery?

Given an HTML document and the task is to enable or disable the form submit button in jQuery. Approach: To enable or disable the form submit button, we use the below code snippet. $('#enabled'). click(function () { if ($('#submit-button').is(':disabled')) { $('#submit-button').

How do I disable and enable a button?

To disable a button using only JavaScript you need to set its disabled property to false . For example: element. disabled = true . And to enable a button we would do the opposite by setting the disabled JavaScript property to false .

Is button disabled jQuery?

Checking if the button is disabled or enabled with jQuery removeAttr('disabled'); }; const checkButton = (e) => { const isDisabled = $('#my-button'). prop('disabled'); if( isDisabled ){ alert("Is disabled!"); } else{ alert("Is enabled"); } }; $(document).


1 Answers

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>
like image 51
Roko C. Buljan Avatar answered Sep 30 '22 22:09

Roko C. Buljan