I'm trying to concatenate a string from certain inputs in a form, and populate another input with that string. I've included my script below, and linked to a js fiddle.
I think the code inside the conditionals in the each() is too redundant, but I can't seem to make it work any other way. Any suggestions are appreciated.
var $namers = $(".namer");
$namers.on('change', function () {
var length = $namers.length - 1;
var nameString = "";
$namers.each(function (i) {
var delimiter = "";
if ($(this).find(":selected").attr('value')) {
if (i < length) delimiter = ": ";
thisVal = $(this).find(":selected").text();
nameString = nameString + thisVal + delimiter;
} else if ($(this).is("input") && $(this).val()) {
if (i < length && $(this).hasClass("from")) delimiter = "-";
thisVal = $(this).val();
nameString = nameString + thisVal + delimiter;
}
});
$("#summary").val(nameString);
});
Here is my original: http://jsfiddle.net/3HsQW/
And a first stab at improving things using an array, which I'm not sure is much better. http://jsfiddle.net/3HsQW/1/