1

i hv written this piece of code. but it is for html checkbox and txtbox. i want to hv the same functionality with asp.net txtbox and checkbox.how to do it?

<script type="text/javascript">
    $(document).ready(function() {
    var checkbox = $('#notify');
        var textfield = $('#notifyemailaddress');
        checkbox.click(function() {
            if (checkbox.is(':checked')) {
                textfield.removeAttr("disabled");
                textfield.removeClass("email");
            }
            else {
                textfield.attr("disabled", "disabled");
                textfield.addClass("email");
            }
        });
    });
</script>
<div>
                               <input type="checkbox"  id="notify"/>
                              <label for="notify">Notify</label>
                              <input type="text" id="notifyemailaddress" disabled="disabled" class="email" style="width:25%" /> 
                             </div>
Ben
  • 54,723
  • 49
  • 178
  • 224
koin
  • 13
  • 2
  • Checkout this [example](http://www.dotnetspider.com/resources/40877-Enable-disable-textbox-when-checked.aspx) – KMån Nov 03 '10 at 08:29

2 Answers2

1

You need to find the correct id on asp.net and to do that you can use the ClientID of your asp.net controls. So you write something like

 var checkbox = $('#<%=notify.ClientID%>');

Where <%=notify.ClientID%>, is print the final rendered id of your control, and the input is looks like

<asp:CheckBox runat="server" id="notify" />

On asp.net 4 you have also the ability to use static id to even avoid the ClientID, but take care to not use the same control id more than one time on the same page. You can read more on: http://msdn.microsoft.com/en-us/library/1d04y8ss.aspx

Aristos
  • 66,005
  • 16
  • 114
  • 150
  • @koin yes the idea is works, check for other details, as for javascript errors. Also debug your javascript on browser to see what you can not find – Aristos Nov 03 '10 at 10:31
  • Does this sort of thing work in separate .js files, or only in the ASPX file? – Andrew Gee Feb 18 '11 at 16:25
  • @Andrew the ClientID, need to be compiled, so you can not place it on js script files that are static, also need the 'notify' control to be found. To place some similar code on js file try something like http://stackoverflow.com/questions/2500001/get-clientid-in-user-control-from-external-javascript-file/2500323#2500323 – Aristos Feb 18 '11 at 17:22
0

If you checkbox server control name is notifyemailaddress then use this:

var textfield = $('#<%= notifyemailaddress.ClientID %>');
wassertim
  • 3,116
  • 2
  • 24
  • 39