Jquery/bootstrap disable button

28,637

Solution 1

If you are using twitter bootstrap there is this exactly behavior already implemented for you.

You just need to change the class .disabled of the element you want to disable.

Like:

$("#previous").addClass('disabled');

Solution 2

Let me show a simple example. Here, a button becomes disabled once you click it.

<button class="btn btn-danger">Click Me</button>

$('.btn-danger').on('click', function() {

    if ($(this).hasClass('disabled')) {
        return false;
    } else {
        $(this).addClass('disabled');
    }
});

Solution 3

Here is my solution:

Javascript

jQuery.fn.extend( {

    bsButtonSetStatus : function( status ) {
        if ( status ) {
            this.removeClass( 'disabled' );
        } else {
            this.addClass( 'disabled' );
        }
    },

} );

CSS

.btn.disabled,
.btn:disabled {
    -webkit-filter: grayscale(100%); /* Safari 6.0 - 9.0 */
    filter: grayscale(100%);
    opacity: 0.25;
    pointer-events: none;
}
Share:
28,637
D3181
Author by

D3181

Updated on November 12, 2020

Comments

  • D3181
    D3181 over 3 years

    Hey iv been looking for a solution using JQuery or by modifying the twitter bootstrap class to prevent a button from performing its onclick action aka disabling it. However the results iv found so far have not performed correctly and mostly only give an appearance of being disabled but still perform correctly:

    $("#previous").prop('disabled', true);
    $("#next").prop('disabled', true);
    

    The purpose is to prevent swapping tabs when the tab index is too high or too low. Most other solutions seem to contain huge amounts of what is seemingly unnecessary Javascript for a feature that seems relatively simple.

    Such as this :

    $("#previous").click(function() {
      var me = $(this);
      me.unbind( "click");            
    })
    

    Which is supposed to remove the bound function, however would mean also reattaching the event when the tab index increases. Is there a simpler solution available to temporarily disable a button or just prevent the default action from being carried out without removing the event or not displaying the button element?

    Any help would be appreciated :)

  • Object Manipulator
    Object Manipulator almost 8 years
    Adding such classes isn't the best idea because the user can easily go to the browser's inspect element and remove this "disabled" class. It's the best to write an additional code with return false.
  • Erick Gallani
    Erick Gallani almost 8 years
    All javascript codes runs in the user side. Thinking in a user that will inspect using development tools, this user can change almost anything in the javascript logic. This supposed to be only for better feedback, any type of dangerous behavior should be validate in the backend.
  • nhershy
    nhershy almost 5 years
    Adding the class as @ErickGallani suggests did not remove the click event from the button. What worked for me was doing it the "classic" way: $('#previous').prop("disabled", true);