kinn
kinn

Reputation: 63

checkbox and textbox

there is a checkbox and a textbox. initially the textbox is set disabled and on checking the checkbox the textbox in enabled. i hv tried this but the problem is that it is not working when runat server is added. or i should change javascript logic for asp.net standard controls. or can suggest me a beeter way to do this.

<script type="text/javascript">
 $(document).ready(function() {
    var checkbox = $('#htmlChkNotify');
    var textfield = $('#htmlTxtNotifyemailaddress');
        checkbox.click(function() {
            if (checkbox.is(':checked')) {
                textfield.removeAttr('disabled');
                textfield.removeClass("email");
            }
            else {
                textfield.attr('disabled', 'disabled');
                textfield.addClass("email");
            }
        });
    });


    </script>

<input type="checkbox"  id="htmlChkNotify"/>
<label for="htmlChkNotify">Notify</label>
<input type="text" id="htmlTxtNotifyemailaddress" disabled="disabled" 
       class="email" style="width:25%" />

Upvotes: 1

Views: 513

Answers (3)

Jacques
Jacques

Reputation: 7135

  1. add the runat attribute to your textbox
  2. Change your 4th line to be var textfield = $('#<%=htmlTxtNotifyemailaddress.ClientID %>');

Should do it

Upvotes: 1

Oded
Oded

Reputation: 498992

There are several way to get this to work.

  1. Use all server side code.
  2. In your client side (javascript) code, instead of using the server side id, use the .ClientId property of the control. So, if your htmlChkNotify is server side, you can use var checkbox = $('#<%=htmlChkNotify.ClientId%>');

Upvotes: 1

Oleg Kalenchuk
Oleg Kalenchuk

Reputation: 517

See generated HTML ("view source" in browser)
and check IDs of checkbox and textbox

Upvotes: 0

Related Questions