Shamik
Shamik

Reputation: 1609

jQuery to get the values of td inside a table

<table id="GridView1">
 <tr>
    <th>KeyWord</th>
    <th>Identifiers</th>
    <th>Values</th>
 </tr>
 <tr>
    <td>
        <select >
           <option selected="selected" value="Action A">Action A</option>
           <option  value="Action b">Action b</option>
           <option value="Action C">Action C</option>
        </select>
    </td>
    <td>
        <textarea >adsasd</textarea>
    </td>
        <td>
        <textarea >dsad</textarea>
    </td>
 </tr>
  <tr>
    <td>
        <select >
           <option value="Action A">Action A</option>
           <option selected="selected" value="Action b">Action b</option>
           <option value="Action C">Action C</option>
        </select>
    </td>
    <td>
        <textarea >asdasd</textarea>
    </td>
        <td>
        <textarea >sdsad</textarea>
    </td>
 </tr>
 </table>

This is the js.

var xml = '<?xml version="1.0" encoding="utf-8"?>';
xml = xml + '<Root>';
i=0;
$("#GridView1 tr").each(function () {

 $(this).find('td').each (function() {
    alert($(this).find('select').val())

  alert($(this).find('textarea').val())


});

});

xml = xml + '</Root>'
alert(xml)

I am getting all the values inside the td but also getting undefined. Any idea why ?

Upvotes: 1

Views: 2408

Answers (3)

Ibrahim Khan
Ibrahim Khan

Reputation: 20750

Find all select and textarea in tr instead of td like following.

$("#GridView1 tr").find('select, textarea').each(function () {
    alert(this.value);
});

Upvotes: 0

Amar Singh
Amar Singh

Reputation: 5622

Demo

$("table").find("td").each(function(){
alert($(this).html());


})

Upvotes: 1

Wa Kai
Wa Kai

Reputation: 466

You get undefined because it tries to display the select value AND the textarea value for EACH td.

To show only the existing values, just check if the element exists within the td:

if($(this).find('textarea').length > 0) {
  alert($(this).find('textarea').val());
}

Upvotes: 2

Related Questions