0 votes
1 view
in Java by (9.2k points)

I’ve the below HTML & JavaScript code:

Html:

<form action="#" method="post">

    <input type="button" name="submit" value="Submit" class="submit_wide" id="myBtn" >

</form>

JavaScript:

$(".submit_wide").click(function () {

    $(this).val('Please wait..');

    $(this).attr('disabled', true);

    setTimeout(function() { 

        $(this).attr('disabled', false);

        $(this).val('Submit');

    }, 2000);

});

I want this code to disable the button for 2 seconds, when the user clicks it which states ‘submit’. Can anyone tell me how to do it? 

1 Answer

0 votes
by (19k points)

You have to refer to the button with a variable inside the setTimeout() call. 

Check the below code implementation:

$(".submit_wide").click(function () {

    var $this = $(this);

    $this.val('Please wait..');

    $this.attr('disabled', true);

    setTimeout(function() { 

        $this.attr('disabled', false);

        $this.val('Submit');

    }, 2000);

});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<input type="button" class="submit_wide" value="Submit"/>

Interested in Java? Check out this Java Certification by Intellipaat.  

Related questions

0 votes
1 answer
asked Apr 15 in Java by sheela_singh (9.2k points)
0 votes
1 answer
0 votes
1 answer
asked Feb 26 in Web Technology by Jake (6.8k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...