How to Disable Button after clicked once using JQuery

No.of Views1989
Bookmarked0 times
Downloads 
Votes0
By  RRaveen   On  19 Jan 2011 10:01:14
Tag : JQuery , How to
In this snippet, I will explain how to disable button after clicked once using JQuery. In the web application we have use the many buttons to do some specific operations.But sometimes users click many times in same button, this is giving unexpected results. So this snippet gives solutions to disable once click button and prevent unexpected result.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this snippet, I will explain how to disable button after clicked once using JQuery. In the web application we have use the many buttons to do some specific operations.But sometimes users click many times in same button, this is giving unexpected results. So this snippet gives solutions to disable once click button and prevent unexpected result.

Implementation

Create a web application using visual studio and add JQuery Library reference within the page.

Html Code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Disablebuttondemo.aspx.cs"
    Inherits="Disablebuttondemo" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to Disable Button after clicked once using JQuery</title>

    <script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js">  
    </script>

</head>
<body>
    <form id="form1" runat="server">
   
        <button id="btnsave" class="buttons" value="Save">
            Save
        </button>
        <button id="btnClear" class="buttons" value="Save">
            Clear
        </button>
   
    </form>
</body>
</html>

Now add two buttons into the page such as Save and Clear it will be like below.

<body>
    <form id="form1" runat="server">
    <div>
        <button id="btnsave" class="buttons" value="Save">
            Save
        </button>
        <button id="btnClear" class="buttons" value="Save">
            Clear
        </button>
    </div>
    </form>
</body>

And then write simple style, to apply for the buttons.

CSS

<style type="text/css">
        .buttons
        {
            font-size: 12px;
            font-weight: bold;
        }
    </style>

And then write javascript to disable button once click with bind and unbind method in jquery.

<script language="javascript" type="text/javascript">
        $(document).ready(function() {
            $('.buttons').bind('click', function() {
                alert("You have clicked " + $(this).text() + "Button");
                $('.buttons').unbind('click');
            });
        });
    </script>

Now run application and see output as like below, 

Image Loading

First click the button, the click event will raise, but if you are try to click next time immediately, you not able click. This is help and reduces multiple submission of same values to server mistakes.

Live Demo

Live demo for disable after clicked once

Download Sample Project 

Download source files -3 kb

Hopes help and thank you for reading.

 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
Comments
By:samDate Of Posted:7/25/2011 10:55:51 AM
Demo page is not working
Demo page is not working please check
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top