tags:

views:

220

answers:

3

I'm trying to create a form where the a user can click a checkbox and their physical address properties get populated to their billing address properties.

My checkbox looks like this:

<label for="UseAccountAddress">Use Account Address</label>
<input id="UseAccountAddress" type="checkbox" value="true" name="UseAccountAddress"/>

And a sample address looks like this:

<input id="Street1" type="text" value="" name="Street1"/>
<input id="PrimaryAddressLine1" type="hidden" value="123 Hi Street" name="PrimaryAddressLine1"/>

I tried adding this jquery to my form which is non-functional:

<script type="text/javascript">
    $().ready(function() {
        $("UseAccountAddress").click(function() {
            if ($(this).attr("checked")) {
                $("Street1").value = $("PrimaryAddressLine1").value;
                $("Street2").value = $("PrimaryAddressLine2").value;
                $("City").value = $("PrimaryCity").value;
                $("State").value = $("PrimaryStateID").value;
                $("PostalCode").value = $("PrimaryZip").value;
            }
            else {
                $("Street1").value = "";
                $("Street2").value = "";
                $("City").value = "";
                $("State").value = "";
                $("PostalCode").value = "";
            }
        });
    });
</script>

Using Firebug I can see that my .click function above never gets hit. I'm guessing that I must be referring to the ID of the checkbox incorrectly, but I'm not sure what else it should be. Since thats incorrect, then the way I'm referring to the elements inside the if block are probably incorrect also.

Whats the correct syntax for this?

+2  A: 

You should use #UseAccountAddress, and likewise use # elsewhere in your code to indicate to JQuery that you want to operate on the element with a specific ID. Generally "#abc" means select the element with ID abc, ".def" means select the element(s) with CSS class def.

Vinay Sajip
Oddly enough, my first version used the # but it wasn't working. Apparently I had something else wrong because when I tried your suggestion it worked just fine.
Sailing Judo
+2  A: 

You want to use the val() method to both get and set the properties on the jQuery objects, not a value property. You also need to change your selector(s) When you do a selection based on an id you need to qualify it with the hash/pound sign (#).

$().ready(function() {
    $("#UseAccountAddress").click(function() {
        if ($(this).attr("checked")) {
            $("#Street1").val( $("#PrimaryAddressLine1").val() );
            $("#Street2").val( $("#PrimaryAddressLine2").val() );
            $("#City").val( $("#PrimaryCity").val() );
            $("#State").val( $("#PrimaryStateID").val() );
            $("#PostalCode").val( $("#PrimaryZip").val() );
        }
        else {
            $("#Street1").val('');
            $("#Street2").val('');
            $("#City").val('');
            $("#State").val('');
            $("#PostalCode").val('');
        }
    });
});
tvanfosson
great answer, as usual. i was reading and testing while you updated your comment with a code sample. i kept struggling with how to set the value properly. glad i hit refresh on the browser again. :)
Sailing Judo
+1  A: 
<script type="text/javascript">
$(document).ready(function() {
    $("#UseAccountAddress").click(function() {
        if ($(this).is(":checked")) {
            $("#Street1").val($("#PrimaryAddressLine1").val());
            $("#Street2").val($("#PrimaryAddressLine2").val());
            $("#City").val($("#PrimaryCity").val());
            $("#State").val($("#PrimaryStateID").val());
            $("#PostalCode").val($("#PrimaryZip").val());
        } else {
            $("#Street1, #Street2, #City, #State, #PostalCode").val('');
        }
    });
});
</script>
David Hedlund
Depending on how your HTML looks, you may be able to come up with a neater selector for the *else* clause; something along the line of $("#deliveryAddress input[type=text]").val('');
David Hedlund
It is better to use "change", not "click" event handler here. It is more robust, since there are other ways of selecting a checkbox.
maksymko
handy shortcut on the else... thanks.
Sailing Judo
@maksymko - thanks for the tip on the change vrs click
Sailing Judo