Search code examples
javascriptjquerydomfrontendgetelementbyid

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);
}

Solution

  • Should work:

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