views:

51

answers:

1

I am trying to get used to the syntax in JQuery when using MVC 2.

I want to copy a phone number from 1 field to another. However the syntax of the line below is wrong; $("#contractAdministratorContact_Phone").val($("#contactClientContact_Phone").val());

The above fields are defined as

<%: Html.EditorFor(model => model.clientContact.Phone)%>
<%: Html.EditorFor(model => model.contractAdministratorContact.Phone)%>

When I look at the page source, the Ids are contractAdministratorContact_Phone and contactClientContact_Phone

My script I locate at the bottom of my page;

<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        $("#chkCopyContact").click(function () {
            if ($("#chkCopyContact").is(":checked")) {
                alert("in!");
                $("#contractAdministratorContact_Phone").val($("#contactClientContact_Phone").val());
            }
            alert("done!");
        })
    });
</script>

So what should my JQuery set statement be instead?

+1  A: 

Try like this:

$("#contractAdministratorContact_Phone").val($("#clientContact_Phone").val());

Notice that the first textbox id should be clientContact_Phone and not contactClientContact_Phone as in your example (Assuming the Html helper code is correct). Also make sure that the :checked condition evaluates to true so that this code is executed.

Darin Dimitrov
Well done. I should have spotted that myself.
arame3333