How To Highlight A Table Row On Click Using JQuery - Codippa
Maybe your like
Press ESC to close
SearchAny element is highlighted due to its background-color property. By default this property has NO value, hence the element is not highlighted when it is at first added to the page. If we change the value of this property, we are easily able to highlight the element. For highlighting an element (such as a table row) on click, there are a couple of approaches using jQuery:
I. Change the ‘background-color’ property of the row in the click event handler of the row. Code would be:
//attach an onclick event handler to the table rows $(“#myTable tbody tr”).click(function(){ //change the ‘background-color’ property to the desired color. $(this).css(‘background-color’,’ yellow’); });Let’s tweak in:
1. The code alongwith its comments is self-explanatory. 2. Hex code of the color may also be used instead of color name.
II. Add a css class to the application css file which will have the ‘background-property’ property alongwith its value. In the onclick event handler of table row add this class to the clicked table row. Code would be:
//attach an onclick event handler to the table rows $(“#myTable tbody tr”).click(function(){ //add the css class which has the required background color property to the clicked table row $(this).addClass(‘rowColor’); });The css class would have an entry like this:
.rowColor{ background-color:yellow; }Let’s tweak in:
1. Code is self-explanatory. 2. Again, in place of color string value, a hex code of the desired color may also be used.
Categorized in:
jQuery,Tagged in:
highlight row on click, jquery Share Article: Share on Facebook Share on Twitter Share on Email Share on Whatsapp Copy LinkPrevious Article
How to fetch items of a list matching some condition in java / How to partition an Object list based on some field value in java
Next Article
How to add a new row to html table on press of a button using jQuery
More in this CategoryjQuery
jQuery View All Articles 1How to increment the value of an element using jQuery / How to add 1 to an element value using jQuery
2How to attach autocomplete to a text box with a Map returned from Spring controller using jQuery
3How to select an element inside a frame using javascript / Various ways to select an element inside a frame using javascript
4How to select multiple elements in jQuery / How to use single expression to select multiple elements in jQuery
View All ArticlesLeave a Reply Cancel reply
Log In
Article Information
Category: jQuery Updated: January 13, 2025 Reading time: 1 MinTable of Contents
Related Articles
How to increment the value of an element using jQuery / How to add 1 to an element value using jQuery
October 14, 2017How to attach autocomplete to a text box with a Map returned from Spring controller using jQuery
July 15, 2017How to select an element inside a frame using javascript / Various ways to select an element inside a frame using javascript
February 2, 2017How to select multiple elements in jQuery / How to use single expression to select multiple elements in jQuery
December 26, 2016How to check if an element has a class using jQuery / How to check the presence of a class on an element using jQuery
November 29, 2016How to refresh a page using jQuery and javascript
November 26, 2016How to add a new row to html table on press of a button using jQuery
November 11, 2016Automated page speed optimizations for fast site performance
Tag » Add Background Color To Tr Jquery
-
Change Tr Background-color - Jquery - Stack Overflow
-
How To Change The Background Color Of A Row In A Table?
-
Set Background Color For First Table Row In JQuery
-
Alternate Table Rows Color Using JQuery Or CSS - JS-Tutorials
-
Add Background Color And Border To Table Row On Hover Using Jquery
-
HTML Tr Tag - W3Schools
-
How To Change The Background Color Of A TR-row In A TABLE With ...
-
JQuery Change HTML Table Row Color On Hover
-
How To Change Background Color Using JQuery - Nathan Sebhastian
-
Change Background Color With Jquery Code Example - Code Grepper
-
Jquery Change Background Color Of Row
-
Changes Background Color Of Even Rows To Gray And Odd Rows To ...
-
How To Change A Row's Background-color Or Color Comparing Pieces ...
-
Dynamically Change Alternate Row Colors Using JQuery - Pakainfo