Home › Forums › JavaScript › Am I doing this correctly?
- This topic is empty.
-
AuthorPosts
-
March 18, 2010 at 12:39 pm #28445fiveeightfiveMember
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 #72980DemocritusMemberIf 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.
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.