Style jQuery Row Highlighter

/ jQuery / by Paul Robinson / 0 comments
This post was published back on March 20, 2009 and may be outdated. Please use caution when following older tutorials or using older code. After reading be sure to check for newer procedures or updates to code.

So that website has a new feature that they have coined the highlighter, very original. It is quite impressive though. It allows you to highlight all the rows that match a certain criteria. Seeing as is a insurance comparison website it would allow you to highlight the insurers that will give you no claims discount or other such things. I think you get my drift. Here is how to do that sort of effect with jQuery.

A little bit Of CSS

First we need a bit of CSS to highlight our items. I’ve just made a simple CSS class as follows. I’m not going to explain it because it shouldn’t really need explaining. ๐Ÿ˜‰

A little bit of HTML

Normally this would probably be generated by the server using PHP, ASP or whatever. For this example though I’m going to use some static information. You need to place the information into a *shock* Table *shock* which is perfectly fine because it’s tubular data. Of course this will work without the table, but you might need to tweak the CSS a little to make it look nicer, but this effect is mainly aimed at tubular data which is normally held in a table.

Before we get to the table though we need a form for the checkboxes that you press to make the highlighter highlight. Here is what I used:

I’ve gone for a list of singers/bands because I’m obsessed with music. So I’m making a genre highlighter that highlights the artists that sing that certain style of music. One thing to note about the form is that the ID attribute of the inputs must match the classes placed in the coming table rows since that’s how the matching works. You can use shorthand or abbreviated versions of your criteria I’m just using the full version for visual impact during the tutorial.

So next is that table. Here is my example, remember to look out for what I was saying about the ID’s of the inputs matching the classes used in the table rows:

As you can see it’s a very simple table & you would have a lot more info in it that I have, but this is just an example. Now that all the HTML is done we can get on with the jQuery. ๐Ÿ˜‰

That jQuery Magic

That little gem is Google’s javascript API. It allows you to grab the latest version of many different javascript frameworks from Google. I use it on my localhost to stop me having to download all the different frameworks but I guess you could use it on a production site. Just remember if Google go down (as unlikely as that is) then your framwork stops working & your javascript code stops working too.

Anyway onto the javascript.

Ok, first is just a little check to see if any of the checkboxes are checked when the page is loaded. Sometimes if you refresh the page the checkboxes that have been checked remain checked, this stops that. Remember to change the form#genre_selector to whatever your form is called.

Next, again remember to change that forms ID, we target the inputs in the form with a type of checkbox & use each to apply a function to each one. Placing that i in the function assigns the current loop number to that variable. Next we creat a global variable called light followed by the current loops number and assign it to false.

Next we make an click event to trigger when the checkboxes are clicked. If the global variable light, followed by the loop number, is false when the checkbox is clicked we need to make the highlight appear. So we grab the ID of the checkbox using the attr('id') function assigning it to genre (you can customise that), then we select the tr in the table with a class the same as the current value of genre (remember to change that if you customised genre). Then we set that global variable to true. The rest is exactly the opposite & turns off the highlight & sets the global variable to false. This is only ran if the highlight is on when the checkbox is clicked. Remember to change all references to genre, if you want to change that variables name.

If you want a different colored highlight for each group (genre in my case) you can just change both lines that say:


Then make a CSS class for each expected group (genre).

Phew. That was hard to explain. I’m hoping that’s understandable & isn’t a load of gibberish. You can see an example of this by clicking here (it will open in a lightbox). Yes, I know the styling isn’t the best, but I’m not a designer. ๐Ÿ˜›

If you have any questions feel free to ask. ๐Ÿ˜‰

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

I'll keep your WordPress site up-to-date and working to its best.

Find out more