1

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>
1
  • You need to learn Javascript. Commented Feb 28, 2010 at 12:05

3 Answers 3

1
<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.

Sign up to request clarification or add additional context in comments.

2 Comments

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

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"
    );
});

3 Comments

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?
You need to add a handler to the click event to run your code whenever the checkbox is clicked.
can you help me with this please because I'm new with java script and jquery
0

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> 

1 Comment

when I tried your code the text box will have no mask and nothing happen when I check the check box!!! any advice?

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.