Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?

like image 634
JBMac Avatar asked Dec 11 '25 09:12

JBMac


1 Answers

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.

like image 62
dfsq Avatar answered Dec 12 '25 23:12

dfsq