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

3

5 Answers 5

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>

Sign up to request clarification or add additional context in comments.

4 Comments

@lord_linus it's not exactly the way you need it but there are all the tips you need! thx! happy coding
one more problem, I am getting that by using this, I am not able to submit the data,
@lord_linus sorry was away, you fixed that issue?
,I just fixed the issue but not in the proper way, it was just for work , this is my script` $('#register').click(function() { $("#registerbtn").hide(); $("#loadingtext").show(); });and this is my div <div id="registerbtn"> <input type="submit" value="Send Instruction" id="register" name="register"/> </div> <div class="form-actions" style="display:none;" id="loadingtext"> <input type="submit" class="btn btn-primary" icon="ok white" value="Send Instruction" id="register" disabled="disabled"/><?php echo $imghtml=CHtml::image('images/loading.gif');?> </div>`
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 );

Comments

0

To permanently remove the button

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

To restore the button

$('#register').attr('disabled',false);

Comments

0

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

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

1 Comment

actually I want to enable the button again if I am getting any error in the registration
0

Enable disabled properties

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

Disable disabled properties

$('#apply-coupon').attr('disabled', 'disabled' );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.