DarkLightA
DarkLightA

Reputation: 15692

What's wrong in my JavaScript code?

LIVE CODE: http://jsfiddle.net/nnMYN/

I have to "automatically copy the postal address to the home address field if a user checks the "Same as above" checkbox. In addition to this, we'll disable the home address field when the checkbox is checked." (from here) What am I doing wrong?

<html>
    <head>
    </head>
    <body>
        <form>
            <fieldset>

                <legend>Billing Information</legend>

                <p>
                    <label>
                        Postal Address:<br>
                        <textarea name="postaladdress" id="postaladdress"></textarea>
                    </label>
                </p>

                <p>
                    Home Address:<br>
                    <label>
                        <input type="checkbox" name="homepostalcheck" id="homepostalcheck">
                        Same as above
                    </label>
                    <br>
                    <textarea name="homeaddress" id="homeaddress"></textarea>
                </p>

            </fieldset>
        </form>
        <script type="text/javascript">
            var loc = document.getElementById('homepostalcheck');
            var home = document.getElementById('homeaddress');
            loc.onclick = !loc.checked ? function() {home.disabled = true; home.value = post.value; alert(post.value);} : function() {home.disabled = false; home.select();};
        </script>
    </body>
</html>

Upvotes: 0

Views: 307

Answers (2)

David Antaramian
David Antaramian

Reputation: 4173

Just add

var post = document.getElementById('postaladdress');

You keep trying to access the parameter value of the post object, but there is no such post object because you haven't defined it. If you add the above line after your definition of home, you should be set! It worked when I edited it on jsFiddle.

Upvotes: 2

Erik
Erik

Reputation: 1106

Try to put the javascript in function() {

<script type="text/javascript">
    (function() {
        var loc = document.getElementById('homepostalcheck');
        var home = document.getElementById('homeaddress');
        loc.onclick = !loc.checked ? function() {home.disabled = true; home.value = post.value; alert(post.value);} : function() {home.disabled = false; home.select();};
    });
</script>

Upvotes: 0

Related Questions