mdevendar
mdevendar

Reputation: 21

How to enable a button after loading the page

I have to disable a button when the page is loading.

How do I enable the button after fully loading the page, using jQuery or JavaScript?

Upvotes: 2

Views: 9758

Answers (4)

James Fenwick
James Fenwick

Reputation: 2211

This is based on @Bang Dao's answer but using native JavaScript.

Disable the button from the beginning:

<button id="button" disabled>A button</button>

And then listen for the DOMContentLoaded event on document:

document.addEventListener('DOMContentLoaded', function() {
    const button = document.getElementById('button');
    button.disabled = false;
});

Upvotes: 0

Luqman
Luqman

Reputation: 137

add a disabled property for the button and in the script, document ready function remove disabled function.

$("#buttonID").prop('disabled',false)

Upvotes: 0

Sherin Syriac
Sherin Syriac

Reputation: 495

Or you can do

<body onload="load()">

and

<script type="text/javascript">
    function load()
    {
        document.getElementById("buttonID").disabled=true;
    }
</script>

Upvotes: 0

Bang Dao
Bang Dao

Reputation: 5102

Disable any button from the beginning:

<input type="button" disabled="disabled" />

And add the enable to window ready function

$(document).ready(
    function(){
        $("input[type=button]", "<input[type=submit]", "input[type=reset]").each( //add more selector here if you want
            function(){
                if($(this).attr("disabled"))
                    $(this).attr("disabled", false); //enable button again
            }
        );
    }
);

Upvotes: 2

Related Questions