2

I have 4 input textfields, and a single textfield that should collect values from each.

<input type="text" class="collector">

<input type="text" class="collected">
<input type="text" class="collected">
<input type="text" class="collected">
<input type="text" class="collected">

The collected inputs are text and expected to be inserted into collector as is. So NULL should be sent too along with those with values.

The collector expects to look like: Data 1 | Null | Data 3 | Data 4

So Null is accepted when no value is given, simply to maintain the process of explode and implode by PHP.

I am stuck, my jquery only picks one of them, because I don't know how to aggregate the separate collected values into an array:

$('input.collector').val($(".collected").text());

Any hint is very much appreciated. Thanks

2 Answers 2

4

Try this:

$('input.collector').val(function() {
    return $(".collected").map(function() {
        return $(this).val() || 'NULL';
    }).get().join('|');
});

Demo http://jsfiddle.net/DDQ8s/

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, nice answer. Way to combine native js and jquery very well.
2
var text= "";
$(".collected").each(function(){
   text += "|" + $(this).val();
});

$(".collector").val(text.substring(1,text.length));

Example: http://jsfiddle.net/nT8NF/

3 Comments

Thanks, but I got the results: undefined | Data 1 || Data 3 | Data 4. Its okay that Null is just empty. But out of 4 fields I recieved 5 (strangely the first is always: undefined). Thanks again
I just updated the code give it another try and take a look at the fiddle. I wrote that off the top of my head.
Yes, working perfectly now. I need a Null better than empty though for end user readablity, although empty is more efficient. Thanks a lot

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.