Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k 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 (19.7k 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, 2021 in Java by sheela_singh (9.5k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Feb 26, 2021 in Web Technology by Jake (7k points)

Browse Categories

...