0

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


    });
});

4 Answers 4

2

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

$('#Voltes5').val(val[0] + ' ' + val.slice(1).join(', '))
Sign up to request clarification or add additional context in comments.

Comments

2

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/

Comments

1

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

Comments

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

demo

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.