tags:

views:

43

answers:

2

I have textbox and checkbox in asp.net page. if the check box checked should have an input mask and if its uncheck should have another input mask. my code is always picking up second condition and I've been advised that I should add click event to run the code whenever the checkbox is clicked and because I'm new with jquery i need help to add click event to my script.

please help.

here is my code:

 <%@ Page Title="" Language="C#" MasterPageFile="~/Imam.Master" AutoEventWireup="true" 
    CodeBehind="WebForm4.aspx.cs" Inherits="Imam_Contacts.WebForm4" %>
 <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">

 <script src="js/jquery-1.4.1.js" type="text/javascript"></script>


<script src="js/jquery.maskedinput-1.2.2.js" type="text/javascript"></script>
  <script type="text/javascript">



  $(function() {
 if ($('#chkhtml:checked').length > 0) {
   $("#txthtml").mask("999-99-9999");
  } else { 
   $("#txthtml").mask("99/99/9999");
  }
 }); 

</script>


</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

<input id="chkhtml" type="checkbox" checked="checked" />

 </asp:Content>
+1  A: 

You have to do

 <script src="js/jquery.maskedinput-1.2.2.js" type="text/javascript"></script> 
  <script type="text/javascript"> 

   $(function() { 
     if ($('#chkhtml:checked')) { 
      $("#txthtml").mask("999-99-9999"); 
     } else {  
        $("#txthtml").mask("99/99/9999"); 
     } 
  });  

</script>
Nasser Hadjloo
when I used your code it only invoked first part of the code and when i check the box nothing happen.i think i need to add click even!!!can u hellp me with that or tell me what i should do??
Eyla
A: 

This code works for me.

 $(document).ready(

 function() {

$('#chkhtml').click(

  function() {
  if ($('#chkhtml:checked').length > 0) {
     $("#txthtml").mask("999-99-9999");
 } else {
     $("#txthtml").mask("99/99/9999");
 }
 });

 });
Eyla