0

Hi, I have following java script function:

function EnableDisableTextBox(chkBoxId, txtBoxId) {
    var isChk = document.getElementById(chkBoxId);
    document.getElementById(txtBoxId).disabled = !(isChk.checked);
}

When I am trying to call above function, by clicking check box its not working as expected

<asp:CheckBox ID="chkBachelors"
      onclick="javascript:EnableDisableTextBox('chkBachelors','txtFirstDegree');"
      runat="server" Text='<%$Resources:Resource, FirstDegree %>' TextAlign="Left"/>

<asp:TextBox ID="txtFirstDegree" CssClass="form-text" runat="server" 
      MaxLength="250">&lt;/asp:TextBox>

Expected Result (When user click chkBachelors check box):

if "chkBachelors" check box is checked 
    then enable "txtFirstDegree" text box
else 
    disable "txtFirstDegree" text box   

What is the problem and how to solve it?

1
  • which asp.net version? and do you mind using jquery? Commented Apr 11, 2012 at 6:41

2 Answers 2

1
    <asp:CheckBox ID="chkBachelors"
              onclick="EnableDisableTextBox(this);"
              runat="server" Text='' TextAlign="Left"/>

        <asp:TextBox ID="txtFirstDegree" CssClass="form-text" runat="server" 
              MaxLength="250"></asp:TextBox>


<script language ="javascript" type="text/javascript">
       function EnableDisableTextBox(checkbox)
       {
           var txtBoxId= "<%=txtFirstDegree.ClientID%>";
           document.getElementById(txtBoxId).disabled = !(checkbox.checked);
       }
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

thank you dhinesh but what if i have multiple pair of such checkbox and text box?
@Vipul: UserControl would be best option.
0

<%=chkBachelors.ClientID%> and <%=txtFirstDegree.ClientID%> will give the client side ID of the Asp.Net controls .You don't need to pass them explicitly

function EnableDisableTextBox() {
            var isChk = document.getElementById(<%=chkBachelors.ClientID%> );
            document.getElementById(<%=txtFirstDegree.ClientID%>).disabled = !(isChk.checked);
        }

5 Comments

you mean to say<br> instead of onclick="javascript:EnableDisableTextBox('chkBachelors','txtFirstDegree');" i have to use onclick="javascript:EnableDisableTextBox('<%=chkBachelors.ClientID%>','<%=txtFirstDegree.ClientID%>');"
why do you need to pass them in EnableDisableTextBox.Moreover the ID here is serverside ant it won't be same at client side unless you set CLIENTIDMODE to static which is applicable only in .net 4.0.
No need to pass IDs as parameters unless the checkbox and textbox controls is within repeater or datagrid
function which you have given is good but only when i have single pair of such checkbox and textbox but in my case i have 10 pairs and i dont wann write same function again and again for each pair
sorry..probably whatever you had in the first comment should work in that case

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.