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

2
  • 5
    What's not working? You're not even calling $.makeArray()! Commented Jul 9, 2010 at 18:43
  • how would i do it with makeArray? Commented Jul 9, 2010 at 18:47

3 Answers 3

1

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).

Sign up to request clarification or add additional context in comments.

8 Comments

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 - I assume you're referring to my first example before I realized that value was a tr. Try the second.
@Pierluc - You're welcome. And yes, the result will be a true js Array. :o)
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 - 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.
|
1

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

2 Comments

Need a .get() on the end to get the base array instead of the jQuery wrapped one :)
@Pierluc - Instead of .filter() you need .children() or .find() for that.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.