All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Disable Button In jQuery

Last Updated : Mar 11, 2024

How To Disable Button In jQuery

In this article we will show you the solution of how to disable button in jQuery, here we are going to show you example for disable button with the help of prop() method. The prop() method sets or returns properties and values of the selected elements.

So using this method we can set button elements disable property to any state of boolean value ‘either true or false’.

Step By Step Guide On How To Disable Button In jQuery :-

Here we defined html block with two buttons ‘Button, Click Me’ Button have class attribute which is used for shows disable state of button.

In script block we defined ready() method which is automatically starts loading code of within this function when this program opened on browser and within that defined click() method with class ‘btn’ so which is refers when clicks on ‘Click Me’ click() event will load particular process.

There we defined prop() method with ‘disable’ property which is set to ‘true’ state so its disable the ‘Button’ element.

<!DOCTYPE html>
<html>
    <head>
        <title>DISABLE BUTTON USING JQUERY</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                $(".btn").click(function(){
                    $(".btn1").prop('disabled',true);
                });
            });
        </script>
    </head>
    <body>
        <button class="btn1">Button</button>
        <button class="btn">Click Me</button>
    </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Here we imported open source jquery library support file which is needed for coding using jquery in program.
  5. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  6. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  7. In script we defined ready() and click() method within that we used prop() method as we know which is used for set or gets value of property so here we sets ‘disable’ property with value ‘true’ for element ‘Button’ using class ‘btn1’.
  8. So which process will begin when user clicks on button ‘Click Me’ after loading this program on browser.
  9. In html block we defined Button with class attribute ‘btn1’ and Click Me button with class attribute ‘btn’ for specify accurately when using this on script block.
  10. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to disable button in jquery.

Before execution of program we needs to confirm internet connection because then only that jquery file will supports defined jquery codes without error.

When we executes program on browser we can see two buttons ‘Button, Click Me’ user needs to clicks on ‘Click me’ then ‘Button’ will gone to disable state.

I hope this article on how to disable button in jQuery helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪