Table row manipulation using jQuery JavaScript

Advertisement

In a jQuery tutorial, I’ve shown how to display different color in alternate row of table using jQuery. This time I’ve come up with advanced version of same tutorial where you can add different color in alternate row of table whereas you can display a different color when mouse moves over each row of table furthermore you can assign a different color to table row when mouse is clicked in the table row.


View Live Demo

HTML code for table row filtering using jQuery

<table id="mytable" border="0" width="40%">
  <tr><td>Michael</td><td>21</td></tr>
  <tr><td>Sam</td><td>22</td></tr>
</table>

As you can see there is nothing special in the above code where I’ve defined a table with id “mytable”.

CSS code for table row manipulation

.oddtr
{
	background-color:#EFF1F1;
}
.eventr
{
	background-color:#F8F8F8;
}
.trover
{
	background-color: #0099CC;
}
.trclick
{
	background-color: #00CCCC;
}

As you can above code, I’ve defined four different classes with four different color. oddtr for coloring the odd row of table, similarly eventr for even row of table then trover class for displaying different color when mouse moves over the table row i.e on mouse over event. Finally, trclick class for displaying different color of the row when mouse is clicked in the table row. Note that you can change these colors according to your choice.

jQuery’s JavaScript code for table row manipulation

//these two line adds the color to each different row
$("#mytable tr:even").addClass("eventr");;
$("#mytable tr:odd").addClass("oddtr");;
//handle the mouseover , mouseout and click event
$("#mytable tr").mouseover(function() {$(this).addClass("trover");}).mouseout(function() {$(this).removeClass("trover");}).click(function() {$(this).toggleClass("trclick");});

The first two line of jQuery adds two differnt classes to the alternate rows of  table with id “mytable“. The last line of jQuery acts for three differnt on the table row. When mouse is moved over the table row trover class is added to that table row. When mouse moves away form that table row trover class is removed from that particular row and finally when mouse is clicked on tha particular row it toggles the class trclick on that particular row.

Download Full Source Code

Popularity: 13% [?]

Enter your email address and get free tutorials, tips and tricks of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:

Related Posts

» Display different color in alternate row using jquery
» jQuery is the most popular JavaScript and Ajax Framework
» Jquery : Benefits, Examples and Free Ebook
» Which is the best JavaScript and Ajax Framework ?

13 Comments on “Table row manipulation using jQuery JavaScript”

  • Ramesh @ The Geek Stuff wrote on 19 September, 2008, 23:52

    Roshan,

    Excellent. Very simple and easy to implement the table.

    The demo table was very help to visualize this behavior easily while reading your post.

    Ramesh
    The Geek Stuff

  • Roshan wrote on 20 September, 2008, 16:12

    @Ramesh – thank you very much such a nice words

  • kamal wrote on 21 October, 2008, 21:18

    let me to tell u : YOU ARE THE BEST.
    You saved me alot of time and work.
    It’s possible with the Div’s?
    Thanks

  • Roshan wrote on 22 October, 2008, 7:01

    @Kamal – in this example I’ve also used div you can refer this post and use div in above example

    http://roshanbh.com.np/2008/03/different-color-alternate-row-jquery.html

  • kamal wrote on 22 October, 2008, 11:00

    Thanks for informations.

  • deepak wrote on 26 October, 2008, 9:04

    Dear Roshan
    Is there a way to remove the highlight of previously clicked row when i click any other row..

  • العاب wrote on 17 May, 2009, 0:35

    thank you man it’s real very usfull and esay

  • Sushil wrote on 3 August, 2009, 13:33

    Nice article with complete details and download link, Thanks for the help!!!

  • Christopher wrote on 28 August, 2009, 15:50

    Thanks for posting this. I tried to adapt it to what i need, but i have two tables in one html page.

    When I include another table and try to duplicate the jQuery code for table2 for instance, nothing is matched. How can i include 2 tables on the same page and have jQuery be applied to both tables?

    Thank you,
    Christopher

  • Clint Williams wrote on 28 October, 2009, 4:21

    Fantastic! Thanks so much!

  • Khara wrote on 19 March, 2010, 18:20

    Awesome! That was very useful. Thank you so much.

  • Dave wrote on 2 December, 2010, 12:33

    Very very nice indeed!
    100% solved my problem!
    Thanks!

  • Dostava Hrane wrote on 21 February, 2011, 15:56

    Very nice and useful example.
    Thanks

Write a Comment

 


Copyright © 2012 Roshan Bhattarai's Blog. All rights reserved.
Powered by WordPress.org, Custom Theme and ComFi.com Calling Card Company.