motti10
motti10

Reputation: 181

How to get an array of selected <option> values in a Table <td>

I have a table with 10 columns

enter image description here

I am trying to loop through the NAME row to get the 4 selected names from the dropdowns.

I used to only need one, but had to add 3 more. I get one by this code

 var $tds = $(this).find('td').filter(':visible')
                name = $tds.eq(0).children().val()

I just can't work it out... GONE blank.

Any code or help would be appreciated.

Happy Holidays

Upvotes: 0

Views: 214

Answers (2)

DinoMyte
DinoMyte

Reputation: 8868

You can iterate through each select in the first td cell using $.each(). To get to the correct selector, you may use :first instead of eq(0).

var arr = [];
$('td:visible:first select').each(function()
{
  arr.push($(this).val());
});

Example : https://jsfiddle.net/DinoMyte/rv3wwpsf/1/

UPDATE : To find all select values in all rows :

var arr = [];
$('tr').each(function()
{ 
   $(this).find('td:visible:first select').each(function(){
     arr.push($(this).val());
  });
});

alert(arr);

https://jsfiddle.net/DinoMyte/rv3wwpsf/2/

Upvotes: 1

Andrew Brooke
Andrew Brooke

Reputation: 12163

You should be able to use .map

var vals = $tds.eq(0).children('select').map(function() {
    return $(this).val();
}).get();

Fiddle

Upvotes: 1

Related Questions