How to select all anchors with display block jquery
How to select all anchors with display block jquery
Very often or not, you need to select all the elements from within the DOM, that have their display property set to “block”.
Solution
1 2 3 |
$('a').filter(function (index) { return $(this).css("display") === "block"; }) |
Filter function from jQuery to the rescue.
Description: Reduce the set of matched elements to those that match the selector or pass the function’s test.
-
selectorType: SelectorA string containing a selector expression to match the current set of elements against.
-
function(index)Type: Function()A function used as a test for each element in the set.
this
is the current DOM element.
-
elementType: ElementAn element to match the current set of elements against.
-
jQuery objectType: ObjectAn existing jQuery object to match the current set of elements against.
Other example
The result of this next example is a red background for items that are even.
1 |
$( "li" ).filter( ":even" ).css( "background-color", "red" ); |