{"id":114,"date":"2009-03-19T11:45:24","date_gmt":"2009-03-19T11:45:24","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/2009\/03\/alternating_table_rows_with_javascript_and_jquery-shtml\/"},"modified":"2009-03-19T11:45:24","modified_gmt":"2009-03-19T11:45:24","slug":"alternating_table_rows_with_javascript_and_jquery-shtml","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_javascript_and_jquery-shtml\/","title":{"rendered":"Alternating Table Rows With JavaScript And jQuery"},"content":{"rendered":"<p>\nOne of the most popular stories on this website is on <a href=\"\/robblog\/archive\/2004\/9\/Alternating_Table_Rows_With_Template_Toolkit_And_CSS.shtml\">Alternating Table Rows With Template Toolkit And CSS<\/a>. The example I use is how to stripe alternate rows in an HTML table by applying a seperate class to each row.\n<\/p>\n<p>\nAs I&#8217;ve been learning jQuery lately, I thought I&#8217;d update this example to show how to do it using this excellent JavaScript framework.\n<\/p>\n<p>\nFirstly, we need to setup our styles. We&#8217;ll have a <var>lite<\/var> and a <var>dark<\/var> class to use that will just set a different background colour.\n<\/p>\n<div class=\"code\"><code>  &lt;style type=\"text\/css\"&gt;<br \/>\n.lite {background-color: #ddffff;}<br \/>\n.dark {background-color: #aaffff;}<br \/>\n&lt;\/style&gt;<br \/>\n<\/code><\/div>\n<p>\nWe&#8217;ll also need an example table.\n<\/p>\n<div class=\"code\"><code>    &lt;table class=\"stripe\"&gt;<br \/>\n&lt;tbody&gt;<br \/>\n&lt;tr&gt;<br \/>\n&lt;td&gt;1&lt;\/td&gt;&lt;td&gt;One&lt;\/td&gt;<br \/>\n&lt;\/tr&gt;<br \/>\n&lt;tr&gt;<br \/>\n&lt;td&gt;2&lt;\/td&gt;&lt;td&gt;Two&lt;\/td&gt;<br \/>\n&lt;\/tr&gt;<br \/>\n&lt;tr&gt;<br \/>\n&lt;td&gt;3&lt;\/td&gt;&lt;td&gt;Three&lt;\/td&gt;<br \/>\n&lt;\/tr&gt;<br \/>\n&lt;tr&gt;<br \/>\n&lt;td&gt;4&lt;\/td&gt;&lt;td&gt;Four&lt;\/td&gt;<br \/>\n&lt;\/tr&gt;<br \/>\n&lt;tr&gt;<br \/>\n&lt;td&gt;5&lt;\/td&gt;&lt;td&gt;Five&lt;\/td&gt;<br \/>\n&lt;\/tr&gt;<br \/>\n&lt;\/tbody&gt;<br \/>\n&lt;\/table&gt;<br \/>\n<\/code><\/div>\n<p>\nNotice how I have given the table a class of <var>stripe<\/var>. This is important, I&#8217;m going to use jQuery to only select table elements that have this class name, else every table on the page would be striped.\n<\/p>\n<p>\nThe jQuery to achieve the striping is actually very simple.\n<\/p>\n<div class=\"code\"><code>  &lt;script type=\"text\/javascript\" src=\"jquery.js\"&gt;&lt;\/script&gt;<br \/>\n&lt;script type=\"text\/javascript\"&gt;<br \/>\n$(document).ready(function() {<br \/>\n$('table.stripe&gt;tbody&gt;tr:even').addClass(\"lite\");<br \/>\n$('table.stripe&gt;tbody&gt;tr:odd').addClass(\"dark\");<br \/>\n});<br \/>\n&lt;\/script&gt;<br \/>\n<\/code><\/div>\n<p>\nFirstly we added the jQuery library to the page. Next we setup our striping code to only run once the <acronym title=\"Document Object Model\">DOM<\/acronym> is place. This is important as we&#8217;re going to be selecting and manipulating over the DOM next. The next two lines apply the <var>lite<\/var> class to even table rows and the <var>dark<\/var> class to odd table rows. Let&#8217;s look at this in a bit more detail.\n<\/p>\n<p>\nThe selector <code>$('table.stripe&gt;tbody&gt;tr:even')<\/code> is easier to explain backwards. We look for even matching <code>tr<\/code> elements, that have a parent of <code>tbody<\/code>, that has a parent of <code>table<\/code> with a class of <var>stripe<\/var>. The use of the <code>&gt;<\/code> symbol instead of just a space is to make sure the elements are direct parent and child elements instead of just ancestor and descendant. Why is this important? Well if we used a space any nested tables that happened to have an striped ancestor would also be striped. This isn&#8217;t normally what we&#8217;d want to happen.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the most popular stories on this website is on Alternating Table Rows With Template Toolkit And CSS. The example I use is how to stripe alternate rows in an HTML table by applying a seperate class to each row. As I&#8217;ve been learning jQuery lately, I thought I&#8217;d update this example to show &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_javascript_and_jquery-shtml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Alternating Table Rows With JavaScript And jQuery&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[31,32,75,79],"class_list":["post-114","post","type-post","status-publish","format-standard","hentry","category-dev","tag-javascript","tag-jquery","tag-web-development","tag-xhtml"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Alternating Table Rows With JavaScript And jQuery - Robert Price<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_javascript_and_jquery-shtml\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Alternating Table Rows With JavaScript And jQuery - Robert Price\" \/>\n<meta property=\"og:description\" content=\"One of the most popular stories on this website is on Alternating Table Rows With Template Toolkit And CSS. The example I use is how to stripe alternate rows in an HTML table by applying a seperate class to each row. As I&#8217;ve been learning jQuery lately, I thought I&#8217;d update this example to show &hellip; Continue reading &quot;Alternating Table Rows With JavaScript And jQuery&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_javascript_and_jquery-shtml\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2009-03-19T11:45:24+00:00\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/alternating_table_rows_with_javascript_and_jquery-shtml\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/alternating_table_rows_with_javascript_and_jquery-shtml\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Alternating Table Rows With JavaScript And jQuery\",\"datePublished\":\"2009-03-19T11:45:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/alternating_table_rows_with_javascript_and_jquery-shtml\\\/\"},\"wordCount\":314,\"keywords\":[\"JavaScript\",\"jQuery\",\"Web Development\",\"XHTML\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/alternating_table_rows_with_javascript_and_jquery-shtml\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/alternating_table_rows_with_javascript_and_jquery-shtml\\\/\",\"name\":\"Alternating Table Rows With JavaScript And jQuery - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"datePublished\":\"2009-03-19T11:45:24+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/alternating_table_rows_with_javascript_and_jquery-shtml\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/alternating_table_rows_with_javascript_and_jquery-shtml\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/alternating_table_rows_with_javascript_and_jquery-shtml\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Alternating Table Rows With JavaScript And jQuery\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Alternating Table Rows With JavaScript And jQuery - Robert Price","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_javascript_and_jquery-shtml\/","og_locale":"en_GB","og_type":"article","og_title":"Alternating Table Rows With JavaScript And jQuery - Robert Price","og_description":"One of the most popular stories on this website is on Alternating Table Rows With Template Toolkit And CSS. The example I use is how to stripe alternate rows in an HTML table by applying a seperate class to each row. As I&#8217;ve been learning jQuery lately, I thought I&#8217;d update this example to show &hellip; Continue reading \"Alternating Table Rows With JavaScript And jQuery\"","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_javascript_and_jquery-shtml\/","og_site_name":"Robert Price","article_published_time":"2009-03-19T11:45:24+00:00","author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_javascript_and_jquery-shtml\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_javascript_and_jquery-shtml\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Alternating Table Rows With JavaScript And jQuery","datePublished":"2009-03-19T11:45:24+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_javascript_and_jquery-shtml\/"},"wordCount":314,"keywords":["JavaScript","jQuery","Web Development","XHTML"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_javascript_and_jquery-shtml\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_javascript_and_jquery-shtml\/","name":"Alternating Table Rows With JavaScript And jQuery - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2009-03-19T11:45:24+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_javascript_and_jquery-shtml\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_javascript_and_jquery-shtml\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_javascript_and_jquery-shtml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Alternating Table Rows With JavaScript And jQuery"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/114","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=114"}],"version-history":[{"count":0,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/114\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}