0

I want to enable a textBox and make it Visible (default settings are false for both) on button click. I can postback but it's inefficient so I want my button to activate onClientClick just to activate and make it Visible.

I tried this:

JS:
function EnableTxt() {
       document.getElementById( '<%=txtSearch.ClientID%>' ).disabled = 'false';
   }
HTML:
<asp:ImageButton ID="btnSearch2" runat="server" ImageUrl="~/Images/search.png" Height="27px" Width="30px" ImageAlign="AbsBottom" OnClientClick="EnableTxt()"/>

<asp:TextBox ID="txtSearch" runat="server" CssClass="btnSearchStyle" Height="27px" Width="134px" onkeypress="return EnterEvent(event)" BackColor="#BDC3C7" BorderStyle="None" Visible="true" Enabled="false"></asp:TextBox>

The JS function that I found doesn't do anything...

2 Answers 2

1

Change your code to-

<asp:ImageButton ID="btnSearch2" runat="server" ImageUrl="~/Images/search.png" Height="27px" Width="30px" ImageAlign="AbsBottom" OnClientClick="if(!EnableTxt()){return false};"/>
    <asp:TextBox ID="txtSearch" runat="server" CssClass="btnSearchStyle" Height="27px" Width="134px" onkeypress="return EnterEvent(event)"  BorderStyle="None" style="visibility:hidden" BackColor="#BDC3C7" Enabled="false"></asp:TextBox>

And change JS as-

 function EnableTxt()
  {
 var id = document.getElementById('<%=txtSearch.ClientID%>');
    if (id.style.visibility === "hidden" && id.getAttribute('disabled'))
    {
         id.style.visibility = "visible";
         id.removeAttribute("disabled");
    }
    else {
         id.style.visibility = "hidden";
         id.setAttribute("disabled");
         }
  }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you!! Do you know how can I make it invisible with second press?
I have edited the code. Just change the JS function to toggle between show and hide. You can up-vote the answer if you found it helpful :)
0

change in js, just remove the disabled attribute:

document.getElementById('<%=txtSearch.ClientID%>').removeAttribute("disabled");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.