MrM
MrM

Reputation: 21989

How to pull value while looping thru a table row?

I have a table that I am looping thru and would like to pull the value from a column in each row. How do I do that?

    $('#buildingList tr').each(function () {
        //check each column in the row and get value from column 3 which is a drop down
    });    

<table id="buildingList"> 
<tr> 
<td class="ms-vb2"/> 
<td class="ms-vb2"/> 
<td class="ms-vb2">
<select id="ddl">
    <option>1</option>
    <option>2</option>
    <option>3</option>
</select> 
</td> 
</tr> 
<tr>  
<td class="ms-vb2"/> 
<td class="ms-vb2"/> 
<td class="ms-vb2">
<select id="ddl">
    <option>1</option>
    <option>2</option>
    <option>3</option>
</select> 
</td> 
</tr> 
<tr> 
<td class="ms-vb2"/> 
<td class="ms-vb2"/> 
<td class="ms-vb2">
<select id="ddl">
    <option>1</option>
    <option>2</option>
    <option>3</option>
</select> 
</td> 
</tr> 
</table> 

Upvotes: 0

Views: 283

Answers (4)

Kris Ivanov
Kris Ivanov

Reputation: 10598

$('#buildingList tr td:nth-child(3) select option:selected').each(function () {
    var myValue = $(this).text();
});

edited for consolidated version

Upvotes: 0

Chandu
Chandu

Reputation: 82903

You can directly get the drop downs in the 3rd column of each row using:

$('#buildingList tr td:eq(2) select').each(function () {    
  var dropDown = $(this); //this refers to the DOM Select
});  

Upvotes: 0

fredrik
fredrik

Reputation: 17617

Depends what you want to do with the values. If you want to store them in an array you can do it by:

var values = [];
$('#buildingList tr select').each(function (index, elem) {
    values.push(elem.value);
});

Also on a side note. You have multiple <select> element's with the same ID. An id-attribute should be unique.

Upvotes: 0

Sean Vieira
Sean Vieira

Reputation: 159905

Use the :selected option and work with the element's value in your each loop:

$('#buildingList td select option:selected').each(function(i, el) {
    // Do something with $(el).val() or el.value
});

Upvotes: 1

Related Questions