Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all text field value located in a specific class

Tags:

jquery

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?

like image 337
chhameed Avatar asked Jul 20 '11 07:07

chhameed


3 Answers

Try this:

$(".test .text-field")

EDIT:

To get values try this:

$(".test .text-field").each(function() {
    alert($(this).val());
});
like image 200
Igor Dymov Avatar answered Nov 04 '22 10:11

Igor Dymov


If you want all the values into an array, you can do this:

var texts= $(".test .text-field").map(function() {
   return $(this).val();
}).get();
like image 45
Rodrigo Almeida Avatar answered Nov 04 '22 10:11

Rodrigo Almeida


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)
like image 3
Grant Miller Avatar answered Nov 04 '22 12:11

Grant Miller