Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

test if display = none

Tags:

jquery

This does not work, should it? Or can you stop the error if another line could do the same:

function doTheHighlightning(searchTerms) {
    // loop through input array of search terms
    myArray = searchTerms.split(" ");
    for(i=0;i<myArray.length;i++)
    {
        // works. this line works if not out commented. Will highlight all words, also in the hidden elements
        //$('tbody').highlight(myArray[i]);

        // not working when trying to skip elements with display none...
        $('tbody').css('display') != 'none').highlight(myArray[i]);
    }

    // set background to yellow for highlighted words
    $(".highlight").css({ backgroundColor: "#FFFF88" });
}

I need to filter rows in a table and color some word. The data has become way to much for the coloring if many words are chosen. So I will try to limit the coloring by only going through the none hidden elements.

like image 409
Tillebeck Avatar asked Jun 04 '10 14:06

Tillebeck


People also ask

How do you check if a div is visible or not?

You can use the jQuery :visible selector to check whether an element is visible in the layout or not. This selector will also select the elements with visibility: hidden; or opacity: 0; , because they preserve space in the layout even they are not visible to the eye.

How do you know if an element is hidden?

Alternatively, you can right-click on the page and select "Inspect" from the menu. In Firefox it's "Inspect element". .is(":hidden") will return true if the selected element is hidden. If it's not hidden, then it will return false .

How do I know if my element is visible on screen?

Summary. Use the getBoundingClientRect() method to get the size of the element and its relative position to the viewport. Compare the position of the element with the viewport height and width to check if the element is visible in the viewport or not.


3 Answers

If you want to get the visible tbody elements, you could do this:

$('tbody:visible').highlight(myArray[i]);

It looks similar to the answer that Agent_9191 gave, but this one removes the space from the selector, which makes it selects the visible tbody elements instead of the visible descendants.


EDIT:

If you specifically wanted to use a test on the display CSS property of the tbody elements, you could do this:

$('tbody').filter(function() {
     return $(this).css('display') != 'none';
}).highlight(myArray[i]);
like image 77
user113716 Avatar answered Oct 02 '22 21:10

user113716


Use like this:

if( $('#foo').is(':visible') ) {
    // it's visible, do something
}
else {
    // it's not visible so do something else
}

Hope it helps!

like image 43
Zuul Avatar answered Oct 02 '22 20:10

Zuul


Try this instead to only select the visible elements under the tbody:

$('tbody :visible').highlight(myArray[i]);
like image 5
Agent_9191 Avatar answered Oct 02 '22 22:10

Agent_9191