{"id":321,"date":"2013-10-03T20:40:07","date_gmt":"2013-10-03T12:40:07","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=321"},"modified":"2018-09-04T13:06:51","modified_gmt":"2018-09-04T05:06:51","slug":"how-to-highlight-html-table-rows-when-mouse-cursor-hovers-over-them-with-jquery","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-highlight-html-table-rows-when-mouse-cursor-hovers-over-them-with-jquery\/","title":{"rendered":"How to highlight HTML table rows when mouse cursor hovers over them with jQuery"},"content":{"rendered":"<p>When I have to look at table of records, I always like to hover my mouse over the row that I am looking at so that I can focus on the details. <\/p>\n<p>In addition, to facilitate my reading of that row, I will want that row to be highlighted as well. <\/p>\n<p>This post describes my exploration to highlight HTML table rows with jQuery.<\/p>\n<h2>The css class to style the highlighted row<\/h2>\n<p>I first define a css class, <code>rowHighlight<\/code>, to be used for highlighting the hovered table rows:<\/p>\n<pre class=\"brush: css; title: ; notranslate\" title=\"\">\r\n.rowHighlight {\r\n    background-color: #f1faff;\r\n}\r\n<\/pre>\n<h2>The jQuery codes for registering interests in mouse hovering event<\/h2>\n<p>I then code the jQuery codes to register listeners to the HTML table rows. These listeners will be notified when my mouse cursor hovers over and away from the table rows. <\/p>\n<p>When the cursor hovers over the rows, I add the CSS class, <code>rowHighlight<\/code>, to the table rows. When the mouse cursor hovers away from the table rows, I remove the css class from the table rows.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n$(document).ready(\r\n\tfunction () {\r\n\t\t$('tr').hover(\r\n\t\t\tfunction(event) {\r\n\t\t\t\t$(this).addClass('rowHighlight');\r\n\t\t\t}\r\n\t\t\t, \r\n\t\t\tfunction(event) {\r\n\t\t\t\t$(this).removeClass('rowHighlight');\r\n\t\t\t}\r\n\t\t);\r\n\t}\r\n);\r\n\r\n<\/pre>\n<h2>The entire HTML document<\/h2>\n<p>The web page is pretty easy to construct - I head over to <a href=\"http:\/\/www.techcoil.com\/blog\/code-segments-for-rendering-html-4-0-pages\/\" title=\"Code segments for rendering html 4.0 pages\" target=\"_blank\">my html reference<\/a> and did a few copying and pasting. I then add the CSS and jQuery codes that I had prepared earlier in the HTML head section.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;!DOCTYPE html PUBLIC &quot;-\/\/W3C\/\/DTD XHTML 1.0 Strict\/\/EN&quot; \r\n    &quot;http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-strict.dtd&quot;&gt;\r\n&lt;html xmlns=&quot;http:\/\/www.w3.org\/1999\/xhtml&quot; xml:lang=&quot;en&quot; lang=&quot;en&quot;&gt;\r\n&lt;head&gt;\r\n    &lt;title&gt;\r\n           HTML table rows highlighted with jQuery when \r\n           mouse cursor hovers over them\r\n    &lt;\/title&gt;\t\r\n    &lt;script type=&quot;text\/javascript&quot; src=&quot;http:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.7.1\/jquery.min.js&quot;&gt;&lt;\/script&gt;\r\n\t&lt;script type=&quot;text\/javascript&quot;&gt;\r\n\t\t$(document).ready(\r\n\t\t\tfunction () {\r\n\t\t\t\t$('tr').hover(\r\n\t\t\t\t\tfunction(event) {\r\n\t\t\t\t\t\t$(this).addClass('rowHighlight');\r\n\t\t\t\t\t}\r\n\t\t\t\t\t, \r\n\t\t\t\t\tfunction(event) {\r\n\t\t\t\t\t\t$(this).removeClass('rowHighlight');\r\n\t\t\t\t\t}\r\n\t\t\t\t);\r\n\t\t\t}\r\n\t\t);\r\n\t&lt;\/script&gt;\r\n\t&lt;style type=&quot;text\/css&quot;&gt;\r\n\t\t.rowHighlight {\r\n\t\t\tbackground-color: #f1faff;\r\n\t\t}\r\n\t&lt;\/style&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n    &lt;table&gt;\r\n\t&lt;tr&gt;\r\n\t\t&lt;th&gt;Fruit&lt;\/th&gt;\r\n\t\t&lt;th&gt;Color&lt;\/th&gt;\r\n\t&lt;\/tr&gt;\r\n\t&lt;tr&gt;\r\n\t\t&lt;td&gt;Apple&lt;\/td&gt;\r\n\t\t&lt;td&gt;Red&lt;\/td&gt;\r\n\t&lt;\/tr&gt;\r\n\t&lt;tr&gt;\r\n\t\t&lt;td&gt;Kiwi&lt;\/td&gt;\r\n\t\t&lt;td&gt;Green&lt;\/td&gt;\r\n\t&lt;\/tr&gt;\r\n\t&lt;tr&gt;\r\n\t\t&lt;td&gt;Orange&lt;\/td&gt;\r\n\t\t&lt;td&gt;Orange&lt;\/td&gt;\r\n\t&lt;\/tr&gt;\r\n\t&lt;\/table&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<div class=\"callToAction\">\n   <a target=\"_blank\" href=\"http:\/\/www.techcoil.com\/proof-of-concepts\/highlight-html-table-rows-when-mouse-cursor-hovers-over-them-with-jquery\" class=\"readMoreLink\">See this proof of concept<\/a>\n<\/div>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-5b\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-5b&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-5b&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F321&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>When I have to look at table of records, I always like to hover my mouse over the row that I am looking at so that I can focus on the details. <\/p>\n<p>In addition, to facilitate my inspection of the data, I will want that row to be highlighted as well. <\/p>\n<p>This post describe my exploration to highlight HTML table rows with jQuery.<\/p>\n","protected":false},"author":1,"featured_media":1208,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[16,89,171],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/jquery-logo-mark-dark.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-5b","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/321"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=321"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/321\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1208"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}