views:

86

answers:

2

I dont understand why the following code is not working. I save some inputs values to sessions. In the next page, I want users be able to copy these values to input fields.

$(function() { $("input#copyshipping").click(function(){

    if ($("input#copyshipping").is(':checked'))

    {  
        // Checked, copy values  
        $("input#BillingFirstName").val("<%=Session("ShippingFIRSTNAME")%>");

        $("input#BillingLastName").val("<%=Session("ShippingLASTNAME")%>");

      $("input#BillingAddress1").val("<%=Session("ShippingADDRESS1")%>");

        $("input#BillingAddress2").val("<%=Session("ShippingADDRESS2")%>");

        $("input#BillingCity").val("<%=Session("ShippingCITY")%>");

      $("input#BillingState").val("<%=Session("ShippingSTATE")%>");

        $("input#BillingCountry").val("<%=Session("ShippingPOSTALCODE")%>");

      $("input#BillingPostalCode").val("<%=Session("ShippingCOUNTRY")%>");

    } else {  
        // Clear on uncheck  
        $("input#BillingFirstName").val("");  
      $("input#BillingLastName").val("");  
      $("input#BillingAddress1").val("");  
        $("input#BillingAddress2").val("");  
      $("input#BillingCity").val("");  
      $("input#BillingState").val("");  
        $("input#BillingCountry").val("");  
      $("input#BillingPostalCode").val("");

  }    });  });
A: 

Why not just echo the session data directly into the inputs if the checkbox is checked? There's no reason to echo it into the JavaScript code and then have jQuery populate the form.

Also, this current setup will fail if the user has JavaScript disabled.

John Rasch
He wants the user to be able to pop the stored values into and out of the textboxes as needed. They might change their mind.
JacobM
Ahhh yes, I was confused when he said "on the next page". I thought he was posting this data back to the server, then on the next page checking if the checkbox was checked, and then displaying the data if it were. I also missed the .click() function because for some reason it wasn't code-colored...
John Rasch
+3  A: 

Looking at the source code, it appears that the textboxes do not have id attributes, only name attributes. When you say, for example, $("input#BillingFirstName"), the "#" indicates an ID, and there isn't one there.

Either add an id, or use $("input[name='BillingFirstName']). I recommend adding the id; it's a faster selector.

JacobM
Oh god, how could I miss that. Thank you so much!
Efe