Click here to Skip to main content
15,885,309 members
Articles / All Topics

Quick Tip – Disable/Enable Button by Textbox using jQuery

Rate me:
Please Sign up or sign in to vote.
4.50/5 (4 votes)
16 Sep 2010CPOL 54.3K   2   5
Quick Tip – Disable/Enable Button by Textbox using jQuery I find myself often with a need to disable/enable a button according to client events that occur on web pages. Today I helped to implement this behavior again so I thought it will be nice to share the code I used.

I find myself often with a need to disable/enable a button according to client events that occur on web pages.

Today I helped to implement this behavior again so I thought it will be nice to share the code I used. In this example I’m disabling/enabling a button according to whether a textbox holds some text. I’m using jQuery to make the code simple. Pay attention that this solution can be implemented for other events also.

The Code

$(document).ready(function () {
  $('#txtAgentName').blur(function () {
        if ($.trim(this.value) == "") {
          $('#btnUpdate').attr("disabled", true);
        }
      else {
            $('#btnUpdate').removeAttr("disabled");
      }
    });
});

As you can see I wire up the ready event of the page and hook a blur event to the textbox with the txtAgentName id. In the blur event I check the value of the textbox and if it is empty I disable the button and if it is not I enable the button. The page that this example is taken from is an MVC 2 view which holds the textbox and the button elements:

<input id="btnUpdate" type="submit" value="Update" disabled />
<%= Html.TextBox("txtAgentName") %>

Summary

A lot was written about how jQuery simplify the Javascript code that you write. In this tip I used jQuery to set Html elements appearance when some event occurs. 

This article was originally posted at http://feeds.feedburner.com/GilFinkBlog

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead sparXys
Israel Israel
Gil Fink is a web development expert and ASP.Net/IIS Microsoft MVP. He is the founder and owner of sparXys. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also co-author of several Microsoft Official Courses (MOCs) and training kits, co-author of "Pro Single Page Application Development" book (Apress) and the founder of Front-End.IL Meetup. You can read his publications at his website: http://www.gilfink.net

Comments and Discussions

 
QuestionEnable? Pin
Member 1216969526-Mar-18 23:38
Member 1216969526-Mar-18 23:38 
GeneralMy vote of 5 Pin
Richard Waddell25-Sep-10 9:25
Richard Waddell25-Sep-10 9:25 
Questioncan we make disable or enable to asp:button also Pin
kashyap201021-Sep-10 1:27
kashyap201021-Sep-10 1:27 
AnswerRe: can we make disable or enable to asp:button also Pin
Gil Fink21-Sep-10 3:56
Gil Fink21-Sep-10 3:56 
AnswerRe: can we make disable or enable to asp:button also Pin
JeffBall5-Jan-11 11:59
JeffBall5-Jan-11 11:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.