user17317304319
user17317304319

Reputation: 13

jQuery: each function iteration for a table selector

$(".TableContainer.MyTable table") this selector gives me an array of 3 tables. I do highlighting for my tables first 15 table data rows. I want to do this for all tables within the jQuery selector array.

$('#MyTable tbody tr td').slice(0, 15).addClass("highlight"); is what I use to add class highlight to the first 15 table datas for a single table. How do I use this selector for each table in first selector?

How do I achieve this using .each()?

Upvotes: 1

Views: 54

Answers (1)

charlietfl
charlietfl

Reputation: 171669

You really don't even need each as there are enough selectors and methods available to do what you need in one chain:

$(".TableContainer.MyTable table").find('tr:lt(16) td').addClass("highlight");

The way this works is the initial collection is the 3 tables and internally jQuery will loop over all 3 (using each within jQuery core) and perform the methods that follow

DEMO

Upvotes: 2

Related Questions