a simple jQuery example

How To Style a Table With jQuery

This is a example of how to style a table with jQuery.

Click here to see the demo

The Code :



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Creating Table With jQuery</title>
<script type="text/javascript" src="jquery-1.7.2.min.js">
</script>
<script>
$(function() {
$("#table tr:even").addClass("stripe1");
$("#table tr:odd").addClass("stripe2");

$("#table tr").hover(mouseIn, mouseOut);

function mouseIn(evt) {
$(this).toggleClass("highlight");
}
function mouseOut(evt) {
$(this).toggleClass("highlight");
}

});
</script>
<style type="text/css">
.heading {
font-weight: bold;
}
.stripe1 {
background-color:#9C9;
}
.stripe2 {
background-color:#9CC;
}
.highlight {
font-weight:bold;
background-color:#9F0;
}
</style>
</head>

<body>
<table width="400" id="table">
<tr class="heading">
<td align="center" valign="middle" bgcolor="#CCCCCC">ITEM</td>
<td width="50%" align="center" valign="middle" bgcolor="#CCCCCC">PRICE</td>
</tr>
<tr>
<td>Milk</td>
<td>2.87</td>
</tr>
<tr>
<td>Eggs</td>
<td>1.89</td>
</tr>
<tr>
<td>Bread</td>
<td>6.80</td>
</tr>
<tr>
<td>Pasta</td>
<td>3.45</td>
</tr>
<tr>
<td>Onion</td>
<td>2.56</td>
</tr>
<tr>
<td>Sugar</td>
<td>1.65</td>
</tr>
<tr>
<td>Salt</td>
<td>1.24</td>
</tr>
</table>

</body>
</html>


Leave a Reply

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