Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make button unclickable after it is being clicked?

Tags:

jquery

I want that after my button is clicked, it would be unclickable for a few seconds, how can I do it? I want to write a code for all buttons, not just for a one. So, I need something like that:

$(document).ready(function() {      
    $('input[type="button"]').click(function() {
        $(this).css("hidden: true");
        delay(3);
        $(this).css("hidden: normal");
    })    
})

It is just a pseudo-code, could you give me a hand?

like image 340
good_evening Avatar asked Apr 29 '11 10:04

good_evening


2 Answers

With jQuery...

$(document).ready(function() {      
    $('input[type="button"]').click(function() {
        var input = this;
        input.disabled = true;
        setTimeout(function() {
           input.disabled = false;
        }, 3000);

    });    
});

jsFiddle.

Without jQuery...

document.addEventListener('DOMContentLoaded', () => {      
    document.querySelectorAll('input[type="button"]')
    .addEventListener('click', (e) => {
        let input = e.target;
        input.disabled = true;
        setTimeout(function() {
           input.disabled = false;
        }, 3000);

    });    
});

Alternatively capture the clicks on a persistent ancestor element and check if they're input[type="button"] types. This prevents attaching multiple events for each element.

like image 195
alex Avatar answered Sep 28 '22 01:09

alex


Add the disabled attribute.

$(this).attr('disabled', true);
like image 25
Marwelln Avatar answered Sep 28 '22 02:09

Marwelln