Very simple striped table with row hover effect

JQuery made it very easy to do this task .... and just with basics of JQuery .JQuery allows us to find all the alternate rows of a table with tbody tr:even selector . We just need to apply a class to all the even rows thats it . To give some hover effect we can use mouseover and mouseout events . In mouseover we are just adding a class and in mouseout we are removing a class.


JQuery code :

<script type="text/javascript" language="javascript">
$(document).ready(function() {
// Initialise the table
$("#table-1 tbody tr:even").addClass("even"); //To add a class to even rows
$("#table-1 tbody tr:odd").addClass("odd");
$("#table-1 tbody tr td").addClass("BorderBottom"); //To add horizontal border
$("#table-1 thead tr ").addClass("header");// Add class to header row
$("#table-1 tbody tr").mouseover(function(){
$(this).addClass("over");
});
$("#table-1 tbody tr").mouseout(function(){
$(this).removeClass("over");
});
});
</script>


CSS Used :

.even
{
background-color:#C6DEFF;
}
.odd
{
background-color:#ffffff;
}
.BorderBottom
{
border-bottom:solid 1px blue;
text-align:center;
}
.over
{
background-color:#C6EEFF;
}
.header
{
background-color:Gray;
color:White;
}

The HTML Table

<table id="table-1" align="center" frame="vsides" width="70%" cellspacing="0" cellpadding="2">
<thead>
<tr>
<th>Col1</th><th>Col2</th><th>Col3</th>
</tr>
</thead>
<tbody>
<tr id="1">
<td>1</td><td>One</td><td>some text</td>
</tr>
<tr id="2">
<td>2</td><td>Two</td><td>some text</td>
</tr>
<tr id="3"><td>3</td><td>Three</td><td>some text</td>
</tr>
<tr id="4"><td>4</td><td>Four</td><td>some text</td>
</tr>
<tr id="5">
<td>5</td><td>Five</td><td>some text</td></tr>
<tr id="6">
<td>6</td><td>Six</td><td>some text</td>
</tr>
</tbody>
</table>

Result is the below Table,see the hovering effect:


Col1Col2Col3
1onesome text
2twosome text
3threesome text
4foursome text
5five some text
6six some text

Comments

Popular posts from this blog

Search column names in database

Typing Speed Test