views:

99

answers:

5

Hi

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 colloring if many words are choosen. So I will try to limit the colloring by only going through the none hidden elements.

Hope it make sense :-)

BR. Anders

+1  A: 
$('tbody').find('tr:visible').hightlight(myArray[i]);
jAndy
+3  A: 

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

$('tbody :visible').highlight(myArray[i]);
Agent_9191
For some reason it does not work. When removing the space between tbody and :visible (tbody:visible), then it does work. I can not tell if it should work (I may have some buggy software running, don't know). But thanks a lot for your answer. Sorry, but I marked patricks answer as right even though it was almost a clone of yours ;-)
Tillebeck
+1  A: 

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]);
patrick dw
Hi. The tbody:visible does the trick very nicely. Can search a huge amount of data and only highlight the displayed text. Great.
Tillebeck
@Tillebeck - Just so you understand, a 'space' in a selector is an important operator. It signifies that you are looking for a descendant. So `tbody:visible` looks for `tbody` elements that are `visible`, while `tbody :visible` looks for *descendants* of `tbody` that are `:visible`. Best of luck! :)
patrick dw
A: 

Use like this:

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

Hope it helps!

Zuul
A: 

As @Agent_9191 and @partick mentioned you should use

$('tbody :visible').highlight(myArray[i]); // works for all children of tbody that are visible

or

$('tbody:visible').highlight(myArray[i]); // works for all visible tbodys

Additionally, since you seem to be applying a class to the highlighted words, instead of using jquery to alter the background for all matched highlights, just create a css rule with the background color you need and it gets applied directly once you assign the class.

.highlight { background-color: #FFFF88; }
Gaby
Super. Thanks for the explanation. That may explain why only the latter one works (visible tbodys). And yep. CSS should go into the CSS file and not be defined in various js files. Both for performance but also so the design guys don't get frustrated :-) Will do.
Tillebeck