Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery/bootstrap disable button

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 :)

like image 395
D3181 Avatar asked Jul 15 '16 12:07

D3181


People also ask

How do I mute a button in bootstrap?

Use the . disabled class in Bootstrap to disable a button.

How do I make a button not clickable in jQuery?

To disable a button with jQuery you need to set the disabled property on the button using the prop method. For example $('. my-button'). prop('disabled', true) .

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 check button is disabled or not in jQuery?

$("#deliveryNext"). button('enable');


2 Answers

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');
like image 159
Erick Gallani Avatar answered Sep 23 '22 01:09

Erick Gallani


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');
    }
});
like image 27
Object Manipulator Avatar answered Sep 24 '22 01:09

Object Manipulator