Search code examples
javascriptasp.netonclickonclientclickasplinkbutton

How to Enable Link Button through Javascript it is initially set to Enable = false


I am new to this kind of programming. I am trying to enable a Link Button on enter of any text in a ASP Text box. Basically it is a search text box when it has text the Search link should be enabled otherwise it should be disabled. The Search Link is a Link Button.

I have this code :- Text box:

<asp:TextBox ID="txtSearch" Width="100%" OnKeyUp='javascript:SetButtonStatus();' runat="server"></asp:TextBox>

Search Link Button:

<asp:LinkButton ID="lbtnSearch" runat="server" onclick="lbtnSearch_Click" ClientIDMode="Static">Search</asp:LinkButton>&nbsp;

JS Function :-

 function SetButtonStatus() {
        debugger;
        var searchtxt = document.getElementById('<%=txtSearch.ClientID%>').value;
        if (searchtxt.length >= 1) {
            document.getElementById('<%=lbtnSearch.ClientID%>').disabled = "";

        }
        else {
            document.getElementById('<%=lbtnSearch.ClientID%>').disabled = "disabled";

        }
    }

But this is unfortunately not working. The link button does not get enabled on entry of text in text box.

Any help is appreciated.


Solution

  • i've no knowledge about ASP, but in JavaScript the disabled property have to be true or false

    <input type="input" id="txtSearch" OnKeyUp="toggleable()">
    <input type="input" id="lbtnSearch" disabled>
    <script>
    function toggleable()
    {
        var txtinput = document.getElementById("txtSearch").value;
        document.getElementById("lbtnSearch").disabled = (txtinput.length) ? false : true;
    }
    </script>