Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery get all values from table column

Tags:

I'm looking for a way to populate a select box with unique values from a table column using jQuery. So far my attempts have been futile but I am relatively new to front end development so I'm hoping someone can show me the light.

Cheers.

like image 758
Fishcake Avatar asked Aug 06 '09 09:08

Fishcake


People also ask

How to get values from table in jQuery?

jQuery: code to get TD text value on button click. text() method we get the TD value (table cell value). So our code to get table td text value looks like as written below. $(document). ready(function(){ // code to read selected table row cell data (values).

How to get row column value from DataTable in jQuery?

How to get all rows data from DataTable in jQuery? You can use rows(). data() to get the data for the selected rows.


1 Answers

This should get you started. Note that I use the cell contents for both the value and the text of the option. You may need to change this but it is unclear from the question.

var items=[], options=[];  //Iterate all td's in second column $('#tableId tbody tr td:nth-child(2)').each( function(){    //add item to array    items.push( $(this).text() );        });  //restrict array to unique items var items = $.unique( items );  //iterate unique array and build array of select options $.each( items, function(i, item){     options.push('<option value="' + item + '">' + item + '</option>'); })  //finally empty the select and append the items from the array $('#selectId').empty().append( options.join() ); 
like image 78
redsquare Avatar answered Sep 21 '22 19:09

redsquare