How to use $ .makeArray () with jQuery

Can someone explain to me how to do this, the jquery api really lacks this. What is wrong with the following code?

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

I just want to capture innerHTML / td text and put it in an array

+3
source share
3 answers

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

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

alert(arr);

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

.map()iterates over the elements and adds the return value to the jQuery object. .get()retrieves only an array from a jQuery object.


value - tr. :

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

alert(arr);

, td html, :

var arr = [];

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

console.log(arr);

.push(), , .map() .map() . , jQuery, ( - ).

+1

html() , text() width(), .

HTML , map(), :

var arr = $(value).children('td').map(function() { return $(this).html(); }).get();
alert(arr[0]);   //Alerts HTML of first <td> element
+1

.html():

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

This applies to many jQuery functions that retrieve values. So what happens is that the filter function returns a set of jQuery elements, but your .html () function arrjust calls the html assignment from the first element in the set.

0
source

Source: https://habr.com/ru/post/1753932/


All Articles