Tag Archives: if else

Highlight a Table Row using jQuery CodeUnit 26 JAN 2010

Using jQuery to highlight a table row on mouse over is pretty simple to achieve, and today I’ll quickly demonstrate how you can achieve this neat effect using the addClass and removeClass jQuery functionalities.

First, you need to define your CSS style to be applied to the row you are hovering over, picking out some CSS attributes that you wish to manipulate. In our case, we’re going to be changing the background-color property and thus our CSS style entry looks like this:

.datahighlight {
        background-color: #ffdc87 !important;
}

Next, we force all the rows in our table to be a member of a specific class:

Text Row to be highlighted

Finally, we apply the jQuery code:

      $(document).ready(function(){
		$('.simplehighlight').hover(function(){
			$(this).children().addClass('datahighlight');
		},function(){
			$(this).children().removeClass('datahighlight');
		});
      });

As you can see, all the jQuery magic is doing on mouse over of any element with the class ‘simplehighlight’ applied is adding a class ‘datahighlight’ to all the child elements of that selected element (the one being hovered above), in our case those being the actual cells of the row. Seeing as the !important rule is applied to the simplehighlight definition, those cell backgrounds are forced to the new color definition, giving the appearance of a row highlight.

On mouse out, the datahighlight class is removed, forcing all of the child elements (once again our cells) to return to their normal background color schemes.

It’s simple, easy to implement, and highly effective, as shown by the example below:

PHP: The One Line IF Statement CodeUnit 06 APR 2009

PHPI’m a big fan of doing everything the verbose way when it comes to coding, all my conditional statements are always decked out with the proper tabbing and the full bracketing that they so richly deserve. To me, this makes the code far easier to read and more importantly follow, a fairly important consideration when I think of just how often I have to go back to legacy code and try to figure just what the heck is going on.

But, as will everything in life there are always exceptions, times when you need to break your own rules and get things done quickly, efficiently. So when you don’t have time to fully write out your PHP IF statement, here’s the alternative for you!

The one line, condensed IF statement, known as a ternary operator to those in the business:

$x = ($myvalue == 10) ? "the value is 10" : "the value is not 10";

As you can see in the example above, $x is assigned the result of the conditional check held in the first set of brackets, the results of which are defined by the if and else return values delimited by the : character.

This means that if $myvalue = 10 then the “the value is 10” line will be assigned to $x, and obviously the other way around for cases where $myvalue != 10.

And there you have it. Shorthand indeed that goes a long way in saving you from typing out all those seemingly unnecessarily verbose brackets! :P

Related link: http://en.wikipedia.org/wiki/%3F: