JBMac
JBMac

Reputation: 339

How to get all values of text inputs with jQuery?

I have a table with a column of pre-populated text inputs:

    <table><tr><td>...</td><td><input class="unique_class" value="value1"/></td></tr>...

I gave each input box the same css class that only that element has (unique_class). I was hoping that I could use jQuery.val() to grab an array of all the input values with the css class in the table like this (I have done something similar with checkbox values):

    var values = $('.unique_class').val();

However, I only am getting the value of the last input in the table. Is there a simple 'fast' way to this without a for-loop iteration? This table can get be very large (100+ rows)?

Upvotes: 3

Views: 743

Answers (2)

user4103823
user4103823

Reputation:

Maybe you can try this:

 var values = $('td.unique_class').val();

Upvotes: 0

dfsq
dfsq

Reputation: 193261

To get an array of values you should use $.map method:

var values = $('.unique_class').map(function() {
    return $(this).val();
}).get();

Also note, how you use .get method to convert jQuery array-like collection to actual array.

Upvotes: 4

Related Questions