tags:

views:

64

answers:

3

I'm trying change an input mask for textbox when the the check box has been check or unckecked but the problem that always is picking up the else condation only even the check box it is check or not.

please advice to fix this problem.

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">



  if ($('#chkhtml:checked').size() > 0)
{
jQuery(function($) {

       $("#txthtml").mask("999-99-9999");
   });
 } else { 
 jQuery(function($) {

       $("#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>
A: 

Your code is running before the checkbox is rendered. Therefore, when the code runs, there are no checkboxes.

You need to wrap the entire if block in $(function() { ... }), which will execute the code in the function after the document finishes loading.

For example:

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

Alternatively, you can use an inline conditional:

$(function() {
   $("#txthtml").mask(
        $('#chkhtml:checked').length > 0 ? "999-99-9999" : "99/99/9999"
    );
});
SLaks
Thank for your help,Now with your code when the page load I will have the mask for else condition and when I check the box nothing happen and this happen for both codes.did I miss any attribute to add to the check box tag and what I should do?
Eyla
You need to add a handler to the click event to run your code whenever the checkbox is clicked.
SLaks
can you help me with this please because I'm new with java script and jquery
Eyla
+1  A: 
<script type='text/javascript'>
    $('#chkhtml').click(function(){
        if ($(this).attr('checked'))
            $("#txthtml").mask("999-99-9999");
        else
            $("#txthtml").mask("99/99/9999");
    });

   $(function(){
       $("#txthtml").mask("99/99/9999"); // this is default mask
   });
</script>

I'm guessing you want to change the mask when checkbox changes its state but you don't know how you can achieve this.

Hasan Khan
when I tried your code the text box will have no mask and nothing happen when I check the check box!!!any advice?
Eyla
You can provide a default mask on load. See the modified code.
Hasan Khan
A: 

Along with the other answers, here's an alternate way to do it...

<script type='text/javascript'> 

    $(document).ready(function() {

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

    }); 

</script> 
Scott Ivey
when I tried your code the text box will have no mask and nothing happen when I check the check box!!! any advice?
Eyla