0

My desired output for the below code should be:

a-b-c-d,e-f-g-h,i-j-k-l

Note the comma instead of - after d and h.

But the below code results in

a-b-c-d-e-f-g-h-i-j-k-l,a-b-c-d-e-f-g-h-i-j-k-l,a-b-c-d-e-f-g-h-i-j-k-l,

I tried searching across stack overflow, but did not came across this type of question. Could someone please help me, please do not mark this question as repititive.

<div class="test1 yes">
 <input type="text" class="xyz" value="a" />
 <input type="text" class="xyz"  value="b" />
 <input type="text" class="xyz" value="c" />
 <input type="text" class="xyz" value="d" />
</div>

<div class="test2 yes">
 <input type="text" class="xyz" value="e" />
 <input type="text" class="xyz" value="f" />
 <input type="text" class="xyz" value="g" />
 <input type="text" class="xyz" value="h" />
</div>

<div class="test3 yes">
 <input type="text" class="xyz" value="i" />
 <input type="text" class="xyz" value="j" />
 <input type="text" class="xyz" value="k" />
 <input type="text" class="xyz" value="l" />
</div>

<input type="submit" class='submit'>

  <p class='count'></p> 
 <p class='test'></p> 

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<script>
    $('.submit').click(function(){
        var count = $('.yes').length
        $('.count').text(count);
       
        var texts = "";
        var i = 0;
    while (i < count) {
        texts += $(".xyz").map(function() { return this.value; })
                                   .get().join('-') + ",";
        i++;
    }

    $('.test').text(texts);    

    });
    

</script>

3 Answers 3

1

Loop through the .yes elements, building the array of their .xyz elements and joining it with -, then join the result with ,:

var texts = $(".yes").map(function() {
    return $(this).find(".xyz").map(function() {
        return this.value;
    }).get().join("-");
}).get().join(",");

Live Example:

$('.submit').click(function() {
    var count = $('.yes').length
    $('.count').text(count);
    
    var texts = $(".yes").map(function() {
        return $(this).find(".xyz").map(function() {
            return this.value;
        }).get().join("-");
    }).get().join(",");

    $('.test').text(texts);

});

// a-b-c-d,e-f-g-h,i-j-k-l
<div class="test1 yes">
 <input type="text" class="xyz" value="a" />
 <input type="text" class="xyz"  value="b" />
 <input type="text" class="xyz" value="c" />
 <input type="text" class="xyz" value="d" />
</div>

<div class="test2 yes">
 <input type="text" class="xyz" value="e" />
 <input type="text" class="xyz" value="f" />
 <input type="text" class="xyz" value="g" />
 <input type="text" class="xyz" value="h" />
</div>

<div class="test3 yes">
 <input type="text" class="xyz" value="i" />
 <input type="text" class="xyz" value="j" />
 <input type="text" class="xyz" value="k" />
 <input type="text" class="xyz" value="l" />
</div>

<input type="submit" class='submit'>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

 <p class='count'></p> 
 <p class='test'></p> 

<p style="font-weight: bold;" class="time text-center"></p>

More:

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

1 Comment

Thank you very much sir. It's working..Love from India
0

It's happened because of your +=, which means it will append the result every iteration. My solution is to write only =.

<div class="test1 yes">
 <input type="text" class="xyz" value="a" />
 <input type="text" class="xyz"  value="b" />
 <input type="text" class="xyz" value="c" />
 <input type="text" class="xyz" value="d" />
</div>

<div class="test2 yes">
 <input type="text" class="xyz" value="e" />
 <input type="text" class="xyz" value="f" />
 <input type="text" class="xyz" value="g" />
 <input type="text" class="xyz" value="h" />
</div>

<div class="test3 yes">
 <input type="text" class="xyz" value="i" />
 <input type="text" class="xyz" value="j" />
 <input type="text" class="xyz" value="k" />
 <input type="text" class="xyz" value="l" />
</div>

<input type="submit" class='submit'>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

 <p class='count'></p> 
 <p class='test'></p> 

<p style="font-weight: bold;" class="time text-center"></p>

<script>
    $('.submit').click(function(){
        var count = $('.yes').length
        $('.count').text(count);
       
        var texts = "";
        var i = 0;
    while (i < count) {
        texts = $(".xyz").map(function() { return this.value; })
                                   .get().join('-') + ",";
        i++;
    }

    $('.test').text(texts);    

    });
    

</script>

3 Comments

Code dumps are not useful answers. To make the answer useful, say what you changed, and why.
Yes, I explain that :)
your code is resulting in a-b-c-d-e-f-g-h-i-j-k-l, whereas what I need is a-b-c-d,e-f-g-h,i-j-k-l
0

Instead of

var texts = "";
var i = 0;
while (i < count) {
    texts += $(".xyz").map(function() { return this.value; })
                               .get().join('-') + ",";
    i++;
}

just write

var texts = ''
$('.yes').each(function() {
  textx += $(this).children('.xyz')
                  .map(function() { return this.value; })
                  .get().join('-') + ','
})

1 Comment

This doesn't generate my desired output i.e a-b-c-d,e-f-g-h,i-j-k-l your code generates a-b-c-d-e-f-g-h-j-k-l

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.