views:

50

answers:

3

Can anyone explain me how to do this, jquery's api is really lacking on this. What is wrong in the following code?

   var arr = $(value).filter(function() { return $(this).is("TD"); } ).html();
   alert(arr[1]);

I just want to grab the innerHTML/text of the td and put it in an array

+1  A: 

The html() function, and similar functions like text() and width() return a scalar value for the first matched element.

If you want an array with the HTML contents of every matched element, you should call map(), like this:

var arr = $(value).children('td').map(function() { return $(this).html(); }).get();
alert(arr[0]);   //Alerts HTML of first <td> element
SLaks
Need a `.get()` on the end to get the base array instead of the jQuery wrapped one :)
Nick Craver
that doesnt work, sorry. value is a tr if that can help.
Pierluc
@Pierluc - Instead of `.filter()` you need `.children()` or `.find()` for that.
Nick Craver
+1  A: 

Using .map() with .get() is one way to go:

var arr = $(value).map(function() { 
      var $th = $(this); 
      if($th.is("TD")) return $th.html(); 
}).get();

alert(arr);

I'm not sure what value represents, but if you change the selector to match only td elements, you could simplify the return statement with return $(this).html();.

.map() iterates over the elements, and adds the return value to the jQuery object. .get() retrieves just the array out of the jQuery object.


Sounds like value is a tr. Then you could do this:

var arr = $(value).children('td').map(function() { 
      return  $(this).html(); 
}).get();

alert(arr);

To create an array with each item containing an array of that row's td element's html, you could do this:

var arr = [];

$('tr').each(function() { 
    arr.push($(this).children('td').map(function() {
        return $(this).html();
    }));
}).get();

console.log(arr);

This uses the standard .push() since I don't think that using .map() inside .map() would work. I think when you pass the inner array into the jQuery object, it just adds it to the main array (or something).

patrick dw
arr[0] is true/false and [1] is still undefined. I want a true javascript array as they call it in jquery api. I want to pass that array to the datatable, maybe someone have a better solution?
Pierluc
@Pierluc - I assume you're referring to my first example before I realized that `value` was a `tr`. Try the second.
patrick dw
thanks for the explanation and solution patrick.
Pierluc
@Pierluc - You're welcome. And yes, the result will be a true js Array. :o)
patrick dw
let's say there is many tr tags(containing tds of course). Would it be possible, in a similar way, to turn all the trs>tds into a 2dimension array?
Pierluc
@Pierluc - So you want an array of arrays? I'll update my answer. Give it a try and let me know if it's what you're looking for.
patrick dw
well what I'm trying to do is more something like arr[0,0] (first indexer would be the tr, and second the td)
Pierluc
@Pierluc - I'm not too familiar with other languages, but in the example given, you would access the values in a similar manner `arr[0][0]`. I haven't seen that style in javascript. Another option would be using a javascript object I suppose. Then you could access with `obj["0,0"]` I suppose. Or `obj[00]`.
patrick dw
oh that's exactly what i wanted. That make sens now hehe, I had some intense headache on friday. Thanks for your help!
Pierluc
A: 

The .html() function:

Description: Get the HTML contents of the first element in the set of matched elements.

This is true of many jQuery functions that are retrieving values. So what's happening is that your filter function is returning a set of jQuery elements, but your .html() function is causing arr to just be assigned the html from the first element in the set.

womp