Mr. Fox
Mr. Fox

Reputation: 328

Combining arrays from different inputs

I'm trying to combine mulitple inputs then combine them to form a sentence/phrase. This example will elaborate it more.

INPUT1: Happy
INPUT2: Birthday to you
INPUT4: You belong to the zoo
INPUT5: and the monkey and the donkey
INPUT6: The Gorilla is you

Output:
Happy, Birthday to you, You belong to the zoo, and the monkey and the donkey, The Gorilla is you

Expected Output:
Happy Birthday to you, You belong to the zoo, and the monkey and the donkey, The Gorilla is you

I want to set a condition where in INPUT one will not be joined using comma(,) rather just a space. How can I accomplish this? Here's a fiddle and code. Appreciate all the help thnx

Fiddle: http://jsfiddle.net/hztMj/6/

Code:

<input class="combine" id="input1" disabled="true" value="Happy"></input>
<input class="combine" id="input2" disabled="true" value="Birthday to you"></input>
<input class="combine" id="input3" disabled="true" value="You belong to the zoo"></input>
<input class="combine" id="input4" disabled="true" value="and the monkey and the donkey"></input>
<input class="combine" id="input5" disabled="true" value="The Gorilla is you"></input>
<input class="combine" id="Voltes5" disabled="true" size="75"></input>
<button id="LaserSword">Set</button>

JS

$(document).ready(function () {
    $('#LaserSword').on('click', function () {
        var form = $('.combine').not('#Voltes5');
        var val = form.map(function () {
            var value = $.trim(this.value)
            return value ? value : undefined;
        }).get();
      $('#Voltes5').val(val.join(', '))//part to be improved


    });
});

Upvotes: 0

Views: 74

Answers (4)

schnill
schnill

Reputation: 955

$(document).ready(function () {
    $('#LaserSword').on('click', function () {
        var res = []
        $('.combine').not('#Voltes5').each(function() {
         res.push($(this).val())
        })
        $('#Voltes5').val(res.join(',').replace(/,/, ' '))
    });
});

demo

Upvotes: 0

Demurgos
Demurgos

Reputation: 1682

You can build your sentence this way :

$(document).ready(function () {
    $('#LaserSword').on('click', function () {
        var form = $('.combine').not('#Voltes5');
        var val = "" ;
        form.each(function (i,e) {
            var value = $.trim(this.value) ;
            if(i > 0){ // we don't add a separator for the first element
                val = val + ( i==1 ? " " : ", " ) ;
                // separator according to index
                // (you can easily extend it as you want)
            }

            val = val + (value || "") ;
        });
      $('#Voltes5').val(val)

    });
});

Demo : http://jsfiddle.net/XF7K8/1/

Upvotes: 2

Sujith PS
Sujith PS

Reputation: 4864

You can use something like this :

$(document).ready(function () {
    $('#LaserSword').on('click', function () {
        var form = $('.combine').not('#input1','#Voltes5');
        var val = form.map(function () {
            var value = $.trim(this.value)
            return value ? value : undefined;
        }).get();
        $('#Voltes5').val($('#input1').val()+" "+val.join(', '))


    });
});

DEMO

UPDATE:

In more general ,

$(document).ready(function () {
    $('#LaserSword').on('click', function () {
        var form = $('.combine').not('.spaceNeeded','#Voltes5');
        var val = form.map(function () {
            var value = $.trim(this.value)
            return value ? value : undefined;
        }).get();

        var spaceval = $('.spaceNeeded').map(function () {
            var value = $.trim(this.value)
            return value ? value : undefined;
        }).get();

        $('#Voltes5').val(spaceval.join(' ')+" "+val.join(', '))


    });
});

Add a class spaceNeeded to html elements :

<input class="combine spaceNeeded" id="input1" disabled="true" value="Happy"></input>

DEMO

Upvotes: 1

everag
everag

Reputation: 7662

If this a simple-not-dynamic scenario, you can achive that with a simple .slice()

$('#Voltes5').val(val[0] + ' ' + val.slice(1).join(', '))

Upvotes: 2

Related Questions