Build URL from form fields with JavaScript or jQuery

jQuery has serialize which builds the query string values.

So if you want to do the entire form:

alert($("#form1").serialize());

If you want to do only a few fields, then just make the selector select those fields.

alert($("#variable1, #variable2").serialize());

Use something like...

var inputs = $('#form1').find('input[type=text]').not('#url');
var str = "http://www.base.url/path/file.ext?"
inputs.each(function (i, item) {
    str += encodeURIComponent(item.name) + "=" + encodeURIComponent(item.value) + "&";
});
$('#url').val(str);

This will select all <input>s on in form1 with type='text', and concatenate them into a query string. See encodeURIComponent().


Orrrr.....you could just use .serialize(). Thank you, prodigitalson.