How to Indicate a Selected HTML Table Row with jQuery

Understanding the actions taken on a website is very important for usability purposes, but also personal sanity. It’s common knowledge that if your website drives a visitor insane they’re not going to come back as often,

…at least not AS often anyway.

So here is a great way to give some feedback to a user who selects a row in an HTML table. We’ll do this by using jQuery’s lovely toggleClass.

If you wanted a table on your site to indicate a row has been selected by changing the background color of a selected row, then you would add an ID to the table. The jQuery selector for such a table would look like this,

 

$(‘#table_ID  tbody  tr’)

 

By the way, it’s best to wrap your table rows within a ‘tbody’ tag to separate them from the table’s header information.

The rest of the jQuery looks like this,

 

$(‘#table_ID  tbody  tr’).click(function ()  {

$(this).toggleClass(‘class_name’);

});

 

(this) is a referent to the selected HTML element, in this case, any ‘tr’ inside of a ‘tbody’ attached to the table with the given ID.

Let’s say we defined a CSS class that would change the background color of a selected row and called it .pickme then set our table’s ID to “newtable”, the code would look like this.

 

$(‘#newtable  tbody  tr’).click(function ()  {

$(this).toggleClass(‘pickme’);

});

 

Using the toggleClass function we have found a fun and easy way to indicate to the user a row has been selected. Since the toggle will turn off the .pickme class when it is clicked again, the user gets instant feedback when selecting or deselecting table rows with just a click.

Of course, by giving your tables a class you could make every table on your site behave this way.

Here is a simple example of an HTML table that changes the background color of a row when that row is clicked.

 

<Simpletable>

 

Notice that because we didn’t target the ‘thead‘ tag with our jQuery selector the row inside the ‘thead‘ doesn’t change when it’s clicked.

 

 

 

 

 

 

 

How Not to Become a Silo

@wd45 posted this on Twitter this morning. It’s a good discussion on losing sight of how your specialization fits into the whole of an industry, and how doing that can cause you work (and industry) to turn into a silo. Although the article is about content strategy, it brings up some valid concerns about SEO, organic and how that fits into an SEM strategy.

SEO is only part of the picture.When we lose sight of the forest through the trees, isolation and irrelevancy are soon to follow.

Don’t become a silo.

SEO MOZ Example of Page Optimization

a graphic showing an example of search engine optimization

 

This graphic shows the basics of good page optimization. Keyword density (at least for Google) is not an important factor.

The keyword shows 3 times in the body of the copy which, as far as Google is concerned is enough to indicate relevancy.

The keyword should be the first word to appear after the <title> tag, and any instances of the keyword(s) you can get in the alt attribute of the <img> tag is a definite bonus.