Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get the values of input fields of multiple classes using jquery

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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>

>Solution :

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:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading