0

How can I make this simpler?:

var address = jQuery.trim($("#Address1").val()) + " " + jQuery.trim($("#City").val()) + " " + jQuery.trim($("#State").val()) + " " + jQuery.trim($("#Zip").val());

2 Answers 2

3

Try this:

var address = "";
$("#Address1, #City, #State, #Zip").each(function(){
    address += $.trim($(this).val()) + " ";
});

If you are looking for the form text inputs then you can make it simpler as given below:

var address = "";
$(":text").each(function(){
    address += $.trim($(this).val()) + " ";
});
Sign up to request clarification or add additional context in comments.

Comments

1

Stack items in an array and loop:

var arr = ['Address1', 'City', 'State', 'Zip'];
for(var i=0;i<arr.length;i++){
    arr[i] = $('#'+arr[i]).val();
}
var address = arr.join(' ');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.