How to bulk select <td> using jQuery

I have an HTML table with many rows. I have seven columns. Each cell ( <td>) in the table has an ID attribute with a grid of [x] [y], where x and y represent columns and rows, respectively.

An example <td>:

<td id="grid[2][2]" class="available"...> -- This indicates 3rd column 3rd row 
<td id="grid[2][4]" class="unavailable"...> -- This indicates 3rd column 5th row 

etc.

Now I need to write a query that gives me the number of columns with class = "available" or class = "unavailable". How to write it in jQuery?

So, to get the first column with class = "available" it will be something like this,

(id = grid[0]* and class == "available").size

Help me convert the above (stupid query) into full jQuery.

+3
source share
4 answers

Here

$("td[id^='grid[0]'].available").length

td 0 . .

+7
 $("td[id|=grid].available")

"TDs, , " " " avaiable "

, " ", , .

+1

?

var $column_number = 2;
var $total_for_column_2 = 0;
for(var $i=0;$i<$total_number_of_rows;$i++) {
  if ( $("td[id=grid[+ $i +"]["+ $column_number +"]").hasClass("available") ) {
    $total_for_column2++;
  }
}

, ,

0

, , id , :

$("#myTable").find("td:nth-child(1).available").length

, nth-child 1, 0.

0

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


All Articles