views:

69

answers:

3

Hey,

i have this code. It returns all the td code including the td /td tags. I want it to only return the content/html of the td

<td>Hey</td> 

should give me just

Hey

 

jQuery("#ReportTable", html).each(function (index, tr) {
    arr[index] = jQuery("tbody tr", tr).map(function (index, td) {
        return jQuery(td).html(); 
    });
});

The jQuery code gives me an array looking like this:

arr[0] = {"<td>1</td>", "<td>Hey</td>", "<td>Some data</td>" } 
arr[1] = {"<td>2</td>", "<td>There</td>", "<td>Some other data</td>" }

From html looking like this:

<table id="ReportTable"><tr><td>1</td><td>Hey</td><td>Some data</td></tr><tr><td>2</td><td>There</td><td>Some other data</td></tr></table>

So the array is good except that i only need the html / text inside the td's.

+1  A: 

You need to go down one more level in your selector, like this:

jQuery(".ReportTable", html).each(function (index, tr) {
  arr[index] = jQuery("tbody tr td", tr).map(function (index, td) { return jQuery(td).html(); });
});

Or, just use .text() instead or .html(), like this:

jQuery(".ReportTable", html).each(function (index, tr) {
  arr[index] = jQuery("tbody tr", tr).map(function (index, td) { return jQuery(td).text(); });
});

(I have ".ReportTable" instead of "#ReportTable", as comments noted on the question IDs need to be unique...so you should use class="ReportTable" if there are multiple)

Nick Craver
@Nick - OP is using some confusing names as function parameters. I have a feeling that when OP does `.each(function (index, tr) {...`, for example, he thinks `tr` is selecting a `<tr>` instead of referencing the current iteration value. Likewise with the `td` in `.map(function (index, td)`. Perhaps worth a mention?
patrick dw
@patrick - Added a request for markup to the question, really it can be slimmed down further...hard to tell what he's after for certain though, the question really doesn't give a lot of context.
Nick Craver
@Nick - Good idea. Though based on the fact that OP gave the name `td` to a parameter that references a `<tr>`, I wouldn't be surprised if he thought `td` was causing `.map()` to iterate over the `<td>` elements.
patrick dw
@patrick - Agreed, the variables make it seem like there are some mixed up concepts here
Nick Craver
A: 

So why not select the TD instead of the TR in your selector and work from there? jQuery's html() function leverages htmlElement.innerHtml, so you shouldn't have any problems once you select the correct elements.

spender
+2  A: 

It seems to me that the simplest solution would be to work directly on the TDs instead of selecting their parent first. Does the code below solve the problem? (I could have mis-understood - apologies if that's the case!)

jQuery("#ReportTable td", html).each (function (index) {
  arr[index] = jQuery(this).html();
}
slightlymore
As a side note, I actually prefer to use `$(html).find("#ReportTable td")` - I think it's a little easier to read. Behind the scenes, jQuery treats both the same, so for human readability I use the `find` method :)
slightlymore