7

I have a very simple code that doesn't work and I don't understand what I'm missing.

External resources

<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>

HTML

<p>
    <button id="btn-id" class="btn btn-large btn-primary" type="button" data-loading-text="loading stuff...">Click Me</button>
</p>

JavaScript

$(document).ready(function () {
    $("#btn-id").button();

    $("#btn-id").click(function () {
        //$("#btn-id").attr("disabled",true);
        $(this).button('loading');

        setTimeout(function () {
            //this desn't work. The button resets but it doesn't get disabled afterwards
            $("#btn-id").button('reset').attr("disabled", true);

            //the bellow line works as expected
            //$("#btn-id").button('reset').addClass("btn-success");
        }, 2000);
    });
});

It seems that after button('reset'), I can addClass('someclass') but I can't disable the button anymore.

Why is this not working and how can I fix it?

jsFiddle

1

6 Answers 6

2

Not sure if this is the right way, but you can use a workaround

$(document).ready(function () {
    $("#btn-id").button();

    $("#btn-id").click(function () {
        $(this).button('loading');
        setTimeout(function () {
            $("#btn-id").addClass('btn-success').data('loading-text', 'OK').button('loading')
        }, 2000);
    });
});

FIDDLE

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

3 Comments

that is a great idea and it works. Just waiting for couple more thoughts. If not I will mark this as the right answer.
@AlexSutu actually you can just leave it in the loading state, you don't need that .button('loading') at the end of my line. I just saw that. the loading state is already the disabled state so it's not a big problem
you're right. The workaround was so simple yet I lost couple of hours on this. Not sure though why you can't actually re-add the disabled attribute after reset.
2

The problem is the setTimeout used in the Button.prototype.setState() method:

https://github.com/twitter/bootstrap/blob/master/js/bootstrap-button.js#L46

Here is a workaround for this:

var $btn = $('.btn-save');
$btn.button('reset');
setTimetout(function() {
    $btn.attr('disabled', 'disabled'); // Disables the button correctly.
}, 0);

(Fiddle: http://jsfiddle.net/f0t0n/BKXVA/)


Source: https://github.com/twbs/bootstrap/issues/6242
Credit goes to: https://github.com/f0t0n

Comments

1

You have to add a class 'disabled' on top of the disabled attribute (it works the same way in bootstrap 3) :

$("#btn-id").addClass('btn-success').attr('disabled', 'disabled').addClass('disabled').html('OK');

jsFiddle

Comments

1

The best solution is this :

$(document).ready(function () {
    $btn = $("#btn-id").button();

    $btn.click(function () {
        $(this).data('loading-text', 'Loading wait...');
        $(this).button('loading').delay(1000).queue(function() {
            $(this).button('reset');
            $(this).dequeue();
        });
    });

    // You can callback after completion, essential for those who need to add classes
    $('#reset-button').click(function () {
        $btn.button('reset');
        $btn.dequeue().delay(100).queue(function(){
            console.log('Loading completed');
            // This button
            $(this).addClass('loading-completed');

            // And very important for who callback function, examples:
            /*
            if (typeof callback === 'function') {
                callback(this);
            }
            */
        });// enable button
    });

});//ready 

Comments

0

Use .prop() instead of .attr()

Updated code

$(document).ready(function () {
    $("#btn-id").button();
    
    $("#btn-id").click(function () {
        $("#btn-id").prop("disabled",true);
        $(this).button('loading');
        
        setTimeout(function () {
            //this desn't work. The button resets but it doesn't get disabled afterwards
            $("#btn-id").button('reset').prop("disabled", true);
            
            //the bellow line works as expected
            //$("#btn-id").button('reset').addClass("btn-success");
        }, 2000);
    });
});

jsFiddle demo

2 Comments

Thanks for the quick answer. Same result using prop instead of attr.
the fiddle you provided still doesn't disable the button after reset. Maybe I'm doing something wrong.
0

The good solution proposed here unfortunately does not work for me, so I have rewritten it as follow:

function( element, status ) {        
    if ( status ) {
        element.removeClass( 'bs-disabled' );
    } else {
        element.addClass( 'bs-disabled' );
    }

    function setStatus() {
        if ( status ) {
            element.removeProp( 'disabled' );
        } else {
            element.prop( 'disabled', true );
        }
    };

    setTimeout( setStatus, 500 );
}

where the style is:

.btn.bs-disabled {
    border: black 1px dotted;
    opacity: 0.1;
}

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.