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

Time:03-20

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 >
 <input type="text"  value="a" />
 <input type="text"   value="b" />
 <input type="text"  value="c" />
 <input type="text"  value="d" />
</div>

<div >
 <input type="text"  value="e" />
 <input type="text"  value="f" />
 <input type="text"  value="g" />
 <input type="text"  value="h" />
</div>

<div >
 <input type="text"  value="i" />
 <input type="text"  value="j" />
 <input type="text"  value="k" />
 <input type="text"  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>

CodePudding user response:

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 >
 <input type="text"  value="a" />
 <input type="text"   value="b" />
 <input type="text"  value="c" />
 <input type="text"  value="d" />
</div>

<div >
 <input type="text"  value="e" />
 <input type="text"  value="f" />
 <input type="text"  value="g" />
 <input type="text"  value="h" />
</div>

<div >
 <input type="text"  value="i" />
 <input type="text"  value="j" />
 <input type="text"  value="k" />
 <input type="text"  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;" ></p>

More:

CodePudding user response:

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

<div >
 <input type="text"  value="a" />
 <input type="text"   value="b" />
 <input type="text"  value="c" />
 <input type="text"  value="d" />
</div>

<div >
 <input type="text"  value="e" />
 <input type="text"  value="f" />
 <input type="text"  value="g" />
 <input type="text"  value="h" />
</div>

<div >
 <input type="text"  value="i" />
 <input type="text"  value="j" />
 <input type="text"  value="k" />
 <input type="text"  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;" ></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>

CodePudding user response:

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('-')   ','
})
  • Related