Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Am I doing this correctly?

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #28445

    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.

    #72980
    Democritus
    Member

    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)
  • The forum ‘JavaScript’ is closed to new topics and replies.