Mateusz
Mateusz

Reputation: 334

Combine find/filter function in jQuery while locating DOM elements

I want to find all elements inside #Grid, then inside a first "TR" tag they should have attribute role="row" and attribute role="columnheader". I've got it and it works fine. Example:

var elements = $("#Grid").find("tr").attr("role","row").first().find("th").attr("role", "columnheader");

What I want to do is to filter it only to elements which meet the condition: offsetWidth < scrollWidth

I tried like below, but this is the incorrect result:

var elements = $("#Grid").find("tr").attr("role", "row").first().find("th").attr("role", "columnheader");
var filtered = elements.filter(function () {
    return $(this).offsetWidth < $(this).scrollWidth;
});

I can use this function as well, but I don't really know how to combine it in jQuery:

function isEllipsisActive(e) {
    return (e.offsetWidth < e.scrollWidth);
}

Upvotes: 0

Views: 85

Answers (2)

Daphoque
Daphoque

Reputation: 4678

Don't use $(this), use the element variable existing in the filter function.

var filtered = elements.toArray().filter(function (e) {
    return e.offsetWidth < e.scrollWidth;
});

Upvotes: 1

Jacques Marais
Jacques Marais

Reputation: 2756

Should work:

var filtered = elements.filter(function () {
    return this.offsetWidth < this.scrollWidth;
});

Upvotes: 1

Related Questions