Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable and disable Twitter Bootstrap Button?

I am trying to achieve the scenario of disabling a button on clicking it, and then enabling it again after the ajax request has completed successfully.

The below is the snippet of my code.

Form

<button id="btnSubmit" type="submit" class="btn btn-warning btn-lg">Submit!</button>

Javascript

$('#resForm').validate({
  // disable submit button
  $('#btnSubmit').prop('disabled', true);

  submitHandler: function(form) {
    $.ajax({
      ..... typical ajax stuff .....
      success: function(data) {
        alert(data);
        $('success').html(data);
        // enable reserve button again
        $('#btnSubmit').prop('disabled', false);
      },
      error: function (jqXHR, textStatus, errorThrown) {
        // console.log(jqXHR);
      }
    });
  }
});

It doesn't work. And checking my console on Chrome tells me Uncaught SyntaxError: Unexpected token (. It feels like it's a stupid mistake somewhere and I can't figure it out. I have read that prop is meant for jQuery 1.6.1 and later and I am on 1.8.3.

like image 232
chongzixin Avatar asked Oct 07 '13 08:10

chongzixin


People also ask

How do I toggle a button in Bootstrap?

Add data-toggle="button" to toggle a button's active state. If you're pre-toggling a button, you must manually add the . active class and aria-pressed="true" to the <button> .

How do I make a button disabled?

The disabled attribute is a boolean attribute. When present, it specifies that the button should be disabled. A disabled button is unusable and un-clickable. The disabled attribute can be set to keep a user from clicking on the button until some other condition has been met (like selecting a checkbox, etc.).

How can make button Unclickable in Bootstrap?

Make buttons look inactive by adding the disabled boolean attribute to any <button> element. Disabled buttons using the <a> element behave a bit different: <a> s don't support the disabled attribute, so you must add the .disabled class to make it visually appear disabled.


2 Answers

$('#resForm').validate({


  submitHandler: function(form) {
// disable submit button
  $('#btnSubmit').prop('disabled', true);
    $.ajax({
      ..... typical ajax stuff .....
      success: function(data) {
        alert(data);
        $('success').html(data);
        // enable reserve button again
        $('#btnSubmit').prop('disabled', false);
      },
      error: function (jqXHR, textStatus, errorThrown) {
        // console.log(jqXHR);
      }
    });
  }
});

Try this

like image 103
Shashank Avatar answered Sep 20 '22 20:09

Shashank


From my answer to another post in SO! I can't think a simpler/easier way! ;-)


For Anchor Tags(Links) :

<a href="#delete-modal" class="btn btn-danger" id="delete"> Delete</a>

To enable the Anchor tag:

 $('#delete').removeClass('disabled');
 $('#delete').attr("data-toggle", "modal");

enter image description here


To disable the Anchor tag:

 $('#delete').addClass('disabled');
 $('#delete').removeAttr('data-toggle');

enter image description here

like image 42
oikonomopo Avatar answered Sep 21 '22 20:09

oikonomopo