javascript masked input textbox

I saw this fiddle:   FIDDLE

On this fiddle, when you select a country from the drop down, it applies a different masking to the postal_code input text box.

So when you select USA it applies this to the textbox: $(".postal_code").mask("99999");
when you select Canada it applies this to the text box:  $(".postal_code").mask("a9a-9a9");

I'm transferring this fiddle to a regular html page.

This is my code so far:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>jQuery Masked Input Sample</title>
    <script src="http://code.jquery.com/jquery-1.4.3.js" type="text/javascript"></script>
    <script src="http://jquery-joshbush.googlecode.com/files/jquery.maskedinput-1.2.2.js" type="text/javascript"></script>

    <script type="text/javascript">

        jQuery(function ($) {
            $(".phone").mask("(999)-999-9999");
            $(".zip").mask("99999?-9999");
        });

        $('#country').change(function () {
            //alert($('#country option:selected').val());
            if ($('#country option:selected').val() == 'US') {
                $(".postal_code").unmask("")
                $(".postal_code").mask("99999");
                //alert(' yea ');
            } else {
                $(".postal_code").mask("a9a-9a9");
            }
        });

    </script>

</head>
<body>
    <div id="demo">
        
        Phone: <input class="phone"/><br/>
        Zip: <input class="zip"/>
        
        <select id="country" name="country" class="country">
            <option selected="selected" value="0"></option>
            <option value="CA">Canada</option>
            <option value="US">United States</option>
        </select>
        
        <input type="text" class="postal_code" name="postal_code" />
        
    </div>

</body>
</html>

Open in new window



When I run the page, the masking on the phone and zip textboxes works fine but the masking on the postal_code textbox is not working.

Anyone know what I'm doing wrong?  How can I fix the masking on the postal_code textbox ?
It should work just like the fiddle mentioned above.
LVL 1
maqskywalkerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
Hi,
you simply have to wrap your change function into a document.ready event.

Your Javascript code is running BEFORE the HTML Dom elements are created, therefore there is no event registered :-)

		$(document).ready(function() {
          $('#country').change(function () {
            if ($('#country option:selected').val() == 'US') {
				$(".postal_code").unmask("")
                $(".postal_code").mask("99999");
                //alert(' yea ');
            } else {
                $(".postal_code").mask("a9a-9a9");
            }
        });
		});

Open in new window

Just replace the above part.
HTH
Rainer
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.