tags:

views:

240

answers:

3

i have a html table and i want to basically loop through each row and each cell per row and simply print out the results. The one thing is some of the cells have input boxes, some have select dropdowns and some have raw content inside the TD.

what is the fastest way to simply looop through every cell in an html table and get the result of each cell. for the select dropdowns i would want to capture the value of the select (not the display text).

+1  A: 

Since there's going to be only one element inside each cell, you could do something like:

$("#myTable tr").each(function() {
    // this represents the row
    $("td > *", this).each(function() {
        // nodeName attribute represents the tag - "INPUT" if element is <input>
        // use the type attribute to find out exactly what type 
        // of input element it is - text, password, button, submit, etc..
        if(this.nodeName == "INPUT") {
            console.log($(this).attr("type"));
        }
    });
});

#myTable > tr means get all <tr> elements that are children of some id="myTable".

Likewise, td > * means get all children of the <td> element that's represented by the this object right now. These are all CSS selectors and there are various ways to select an item in the DOM. See the jQuery docs to learn more about selectors.

The jQuery core docs are a great reference to find out about all available methods on the jQuery object.

Anurag
once your are in the loop how do you find out if there is a input box, select or regular text ?
ooo
is there only going to be 1 element or could you have multiple?
Anurag
always going to be one element
ooo
i debug this in firebug and it seems to hit the first line and go right to the end as if it skips over all of the code. are you sure the first line is correct ??
ooo
The child selector `>` is not working for me either in this case. I am going to post a question on SO about it. But weirdly the descendant selector works. And there was also a bracket missing somewhere :) Updated the code.
Anurag
The child selector wasn't working because browsers insert an implicit `tbody` element. So it really should have been `#myTable > tbody > tr` - more on this question http://stackoverflow.com/questions/2075940/is-this-a-bug-in-how-jquery-treats-child-selectors
Anurag
Anurag, have you considered my solution?
Jonathan Sampson
Jonathan, that is a very elegant solution indeed.
Anurag
+1  A: 

It's hard to know exactly what you want, but if you can assume that td's with a select box have only that, td's with an input have only a text field, you could do something like this: http://jsbin.com/alara3/edit

David Hedlund
thanks . .this works great . . is there anyway to know when you move to another row to get current row ?
ooo
+3  A: 

Online demo: http://jsbin.com/ewazu

I'm cycling through each TD first and evaluating whether or the the first-child is an input element. If it is, we return its value. If it's not, we ask if it produces text. If it produces text, we ask for the text. If it doesn't produce text, we request the HTML for the TD:

$("table td").each(function(i,o){
  var value = ( $(":first-child", this).is(":input") ) 
    ? $(":first-child", this).val() 
    : ( $(this).text() != "" ) 
      ? $(this).text() 
      : $(this).html() ;
  alert(value);
});
Jonathan Sampson