I want to get all fields that are located in a single class name, for example my code like.
<div class="test">
<input type="text" class="text-field" />
<input type="text" class="text-field" />
<input type="text" class="text-field" />
<input type="text" class="text-field" />
</div>
<div class="test">
<input type="text" class="text-field" />
<input type="text" class="text-field" />
<input type="text" class="text-field" />
<input type="text" class="text-field" />
</div>
I want to get the each loop
in which it return me the text fields value that is located in this class. Any suggestions?
Try this:
$(".test .text-field")
EDIT:
To get values try this:
$(".test .text-field").each(function() {
alert($(this).val());
});
If you want all the values into an array, you can do this:
var texts= $(".test .text-field").map(function() {
return $(this).val();
}).get();
Here's another method to obtain an array of the input values:
Array.from($('.test .text-field').get(), e => e.value)
Or alternatively:
[].map.call($('.test .text-field').get(), e => e.value)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With