0

I'm trying to get the value of a specific cell in a table in order to filter out unwanted rows. However, when I try to get the value of that cell it always reports null or nothing at all. I've looked at several sources and done my code the same way with no luck. Here is my code:

$("td").each(function () {
    var setName = $(this).find("#headerName").text();
    alert(setName);
    if (setName != weaverSet) {
        $(this).hide();
    }

weaverSet is getting it's value passed from the drop down and working correctly. Here is the code for the part of the table I am trying to get:

 <td id ="headerName">
     @item.WeaverSetName
 </td>

Any help is appreciated on this issue.

tereško
  • 58,060
  • 25
  • 98
  • 150
Danger_Fox
  • 449
  • 2
  • 11
  • 33
  • 2
    You are already iterating over all cells. No cell contains another cell with ID `headerName`, *one of the cells itself* has that ID. You can get the content of the element with the ID with `$('#headerName').text()`. You can get each cell's content with `$(this).text()`. I'm not quite sure what you are trying to achieve, a simple and complete example at http://jsfiddle.net/ would be helpful. – Felix Kling Jan 03 '13 at 17:28
  • This may answer your question: http://stackoverflow.com/questions/376081/how-to-get-a-table-cell-value-using-jquery – Jamal Ali Jan 03 '13 at 17:29
  • @Felix Kling That comments should be the answer – Dharman Jan 03 '13 at 17:30

4 Answers4

0

Try getting the value of the td without calling the find function. Inside your loop get the value like this

var setName = $(this).text();
72lions
  • 680
  • 2
  • 7
  • 12
0

Based on your question, you should most likely be using a class for headerName as opposed to an ID (which there should only be one of on your page).

You could then perform your same iteration.

$("td").each(function () {
     var setName = $('.headerName',this).text();
     alert(setName);
     if (setName != weaverSet) {$(this).hide();}
});

Example

or as they are just elements, you should be able to pull the text from them completely, along with their contents:

$("td").each(function()
{
     var setName = $(this).text();
     alert(setName);
}

Example

Rion Williams
  • 74,820
  • 37
  • 200
  • 327
0

You should use filter("#headerName") instead of find("#headerName")

You are already iterating over all cells. find looks for a cell with and id headerName inside each cell. What you probably wanted is to check if current cell has an id which is done using filter() function.

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

The code you have there would only work if the #headerName cell were inside yet another <td>. Is there just one <td> with the ID headerName? If so, this should work (without the .each() around it):

var cell= $("#headerName");
var setName = cell.text();
            alert(setName);
            if (setName != weaverSet) {
                $(cell).hide();
            }

If not, then that's problematic, because IDs need to be unique, and you should be using classes in that case.

JLRishe
  • 99,490
  • 19
  • 131
  • 169