Enable asp.net disable button - using javascript

In my application, I have a button and a text box. The user enters something into the text box and then clicks the button. I want to:

The search button should stay off when the page loads for the first time. I can achieve this by installing it in the code behind the file.

Now I want it to stay disabled when the user enters up to 2 characters. At the moment when the user types the third character, the button should turn on automatically.

The important thing is that this needs to be done without asp.net AJAX, as this website will be launched from older mobile phones. Thus, only pretty simple javascript or jquery is supported.

Any help would be appreciated.

thank

Varun

+5
source share
3 answers

to use document.getElementById in asp.net and not use the full name, you must provide asp.net for it. Instead:  document.getElementById("ctl00_ctl00_phContent_phPageContent_btnSearch")

Try:

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

Where btnName- asp:Button Id. The code <%=generates an actual button identifier that is fully compliant, so you don’t have to worry about what happens with a hard coded identifier.

+4
source

I got this to work with an HTML text box, I don't think you can do this with an 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>
+3
source

jQuery,

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

,

$(<button selector>).attr('disabled',false).
+1

All Articles