Grow your CSS skills. Land your dream job.

Am I doing this correctly?

  • # March 18, 2010 at 12:39 pm

    I’m creating a form in which a user can copy their billing info over to the shipping info if they happen to be the same. I used this jQuery plugin: http://www.encaffeinated.com/articles/v … th_jquery/ and got it working, but it only worked with one <select> input so I tried to add another. so basically I changed it from:

    Code:
    $(document).ready(function() {

    //when the checkbox is checked or unchecked
    $(‘#copyaddress’).click(function() {

    // If checked
    if ($(“#copyaddress”).is(“:checked”)) {

    //for each input field
    $(‘ul#shipping input’, ‘:visible’, document.body).each(function(i) {
    //copy the values from the billing_fields inputs
    //to the equiv inputs on the shipping_fields
    $(this).val( $(‘#billing input’).eq(i).val() );
    });
    //won’t work on drop downs, so get those values
    var bcountry = $(“#bcountry”).val();
    $(“#scountry”).selectOptions(bcountry);

    } else {

    //for each input field
    $(‘ul#shipping input’, ‘:visible’, document.body).each(function(i) {
    //set shipping_fields inputs to blank
    $(this).val(“”);
    });
    $(“#scountry”).selectOptions(“”);
    }
    });
    });

    to

    Code:
    $(document).ready(function() {

    //when the checkbox is checked or unchecked
    $(‘#copyaddress’).click(function() {

    // If checked
    if ($(“#copyaddress”).is(“:checked”)) {

    //for each input field
    $(‘ul#shipping input’, ‘:visible’, document.body).each(function(i) {
    //copy the values from the billing_fields inputs
    //to the equiv inputs on the shipping_fields
    $(this).val( $(‘#billing input’).eq(i).val() );
    });
    //won’t work on drop downs, so get those values
    var bcountry = $(“#bcountry”).val();
    $(“#scountry”).selectOptions(bcountry);

    var bstate = $(“#bstate”).val();
    $(“#sstate”).selectOptions(bstate);

    } else {

    //for each input field
    $(‘ul#shipping input’, ‘:visible’, document.body).each(function(i) {
    //set shipping_fields inputs to blank
    $(this).val(“”);
    });
    $(“#scountry”).selectOptions(“”);
    }
    });
    });

    Did I add the

    Code:
    var bstate = $(“#bstate”).val();
    $(“#sstate”).selectOptions(bstate);

    part correctly?

    Thanks, I hope I made it clear what my question is.

    # March 26, 2010 at 2:22 am

    If this is the only use of JQuery on your page, you’re being inefficient. I would recommend simply writing a javascript function to accomplish this task in good ‘ol fashion js.

    That checkbox should have an "onclick=yourfunction(this)"

    Then, simply have the function check if this.checked is "checked", and copy the values. Otherwise, clear the fields.

    No need to use a hand grenade for a fly.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".