Enabling Disabling button asp .net - using javascript

16,895

Solution 1

in order to use the document.getElementById in asp.net and not have to use the full name, you should let asp.net provide it. Instead of: document.getElementById("ctl00_ctl00_phContent_phPageContent_btnSearch")

Try:

document.getElementById('<%= btnName.ClientID %>')

Where btnName is the asp:Button Id. The <%= code will generate the actual button id, fully qualified, so you don't have to worry about things changing with the hard coded id.

Solution 2

I got this to work with a HTML text box, I don't think you can do it with a asp.net text box:

<head>
    <script type="text/javascript">
        function TextChange() {
            var t = document.getElementById("Text1");
            var b = document.getElementById("Button1");
            if (t.value.length > 2) {
                b.disabled = false;
            }
            else {
                b.disabled = true; 
            }


        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="Text1" type="text"  onkeyup="TextChange();" />
        <asp:Button ID="Button1"   runat="server" Text="Button" Enabled="False" />
    </div>
    </form>
</body>

Solution 3

If you're using jQuery, use

$(<selector>).val().length 

to get the size, then you can set the button's disabled attribute with

$(<button selector>).attr('disabled',false).
Share:
16,895
Varun Sharma
Author by

Varun Sharma

Updated on June 16, 2022

Comments

  • Varun Sharma
    Varun Sharma almost 2 years

    Currently in my application I am having a button and a text box. The user types something in the textbox and then presses the button. What I want is that:

    The search button should should stay disabled when the page loads for the first time. I can achieve that by setting it to disabled in the code behind file.

    Now I want it to remain disabled when the user types upto 2 characters. The moment the user types third character the button should get enabled automatically.

    The important thing is that it has to be done without asp .net AJAX since this website will be run from old mobile phones. So only pretty basic javascript or jquery is supported.

    Any help will be appreciated.

    Thanks

    Varun

  • Varun Sharma
    Varun Sharma over 12 years
    Hi, thanks for your help but i got it to work using pure javascript for normal forms. But since my textbox and button are inside asp .net contentplaceholder I am unable to get it ID of the button which I want to use it at the runtime. I am forced to write like this: document.getElementById("ctl00_ctl00_phContent_phPageContent‌​_btnSearch").disable‌​d = false; instead of document.getElementById("btnSearch").disabled = false;
  • Varun Sharma
    Varun Sharma over 12 years
    Hi, thanks for your help but i got it to work using pure javascript for normal forms. But since my textbox and button are inside asp .net contentplaceholder I am unable to get it ID of the button which I want to use it at the runtime. I am forced to write like this: document.getElementById("ctl00_ctl00_phContent_phPageContent‌​_btnSearch").disable‌​‌​d = false; instead of document.getElementById("btnSearch").disabled = false;
  • kand
    kand over 12 years
    Oooh I see what the real issue is now. You can access the id of a control by inserting some code behind code in your asp by using: <% = btnSearch.ClientID %> given your button is named btnSearch in the code behind. So, you could do this with jQuery: $("<% = btnSearch.ClientID %>") to get the jQuery object of your button, or you could do this with pure js: document.getElementById("<% = btnSearch.ClientID %>")
  • Lorgarn
    Lorgarn almost 10 years
    How come the expression is alwas the literal String in my case? I never get the actual value...