{"id":214,"date":"2009-07-27T22:27:20","date_gmt":"2009-07-27T21:27:20","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=214"},"modified":"2009-07-27T22:27:20","modified_gmt":"2009-07-27T21:27:20","slug":"styles-alter-with-javascript","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/27\/styles-alter-with-javascript\/","title":{"rendered":"Styles &#8211; alter with javascript"},"content":{"rendered":"<p>To alter style values within a html page via Javascript can be done two different ways, that I know of, one is via getting the object itself and then altering the style from that and the other is via the document style sheets object.<\/p>\n<p>The document style sheets object is all of the styles on the web page and is index within an array manor.  For example<\/p>\n<pre lang=\"javascript\">\r\n       styleSheet = document.styleSheets[<insert number of object>]\r\n<\/pre>\n<p>But since the two main browsers (Internet Explorer and also Firefox) deal with the css rules within this object with a different reference (IE = rules and FF = cssRules).<\/p>\n<pre lang=\"javascript\">\r\n       if (styleSheet.cssRules)\r\n              cssRule = styleSheet.cssRules[0]; \/\/ Firefox\r\n       else if (styleSheet.rules)\r\n              cssRule = styleSheet.rules[0];         \/\/ IE\r\n<\/pre>\n<p>The other method is via the id of the object, to obtain this just need to use the javascript getElementById function with the document object.<\/p>\n<pre lang=\"javascript\">\r\n       cssRule = document.getElementById(style)\r\n<\/pre>\n<p>And then once the object of the style is obtained with both functions can just alter the style of the object with the same code.<\/p>\n<pre lang=\"javascript\">\r\n       if (cssRule.style.backgroundColor == colour)\r\n              cssRule.style.backgroundColor = colour2;\r\n       else\r\n              cssRule.style.backgroundColor = colour;\r\n<\/pre>\n<p>Here is the full code<\/p>\n<pre lang=\"javascript\">\r\n<html>\r\n       <head>\r\n              <style language=\"text\/css\">\r\n                     h1 { background-color: lightblue;}\r\n                     #pstyle { background-color : lightblue;}\r\n              <\/style>       \r\n              <script language=\"javascript\">\r\n                     function alterh1(colour, colour2)\r\n                     {       \r\n                            var styleSheet, cssRule;\r\n                            if (document.styleSheets)\r\n                            {\r\n                                   styleSheet = document.styleSheets[0];\r\n                                   if (styleSheet)\r\n                                   {\r\n                                          if (styleSheet.cssRules)\r\n                                                 cssRule = styleSheet.cssRules[0]; \/\/ Firefox\r\n                                          else if (styleSheet.rules)\r\n                                                 cssRule = styleSheet.rules[0];         \/\/ IE\r\n                                          if (cssRule)\r\n                                          {\r\n                                                 if (cssRule.style.backgroundColor == colour)\r\n                                                        cssRule.style.backgroundColor = colour2;\r\n                                                 else\r\n                                                        cssRule.style.backgroundColor = colour;\r\n                                          }\r\n                                   }\r\n                            }\r\n                     }\r\n              \r\n                     function alterstyle(style,colour, colour2)\r\n                     {\r\n                            var cssRule;\r\n                            cssRule = document.getElementById(style);\r\n                            if (cssRule)\r\n                            {\r\n                                   if (cssRule.style.backgroundColor == colour)\r\n                                          cssRule.style.backgroundColor = colour2;\r\n                                   else\r\n                                          cssRule.style.backgroundColor = colour;\r\n                            }\r\n                     }\r\n              <\/script>\r\n\r\n       <\/head>\r\n       <body>\r\n              <h1>Change this style<\/h1>\r\n              <input type=\"submit\" value=\"click here\" onclick=\"javascript:alterh1('blue', 'lightblue')\"\/>\r\n\r\n              <p id=\"pstyle\">\r\n                     HI there\r\n              \r\n              <input type=\"submit\" value=\"click here\" onclick=\"javascript:alterstyle('pstyle','blue','lightblue')\"\/>       \r\n       <\/body>\r\n       \r\n<\/html> \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>To alter style values within a html page via Javascript can be done two different ways, that I know of, one is via getting the object itself and then altering the style from that and the other is via the document style sheets object. The document style sheets object is all of the styles on &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/27\/styles-alter-with-javascript\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Styles &#8211; alter with javascript<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[402,398,400],"class_list":["post-214","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-javascript","tag-linux","tag-windows"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/214","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=214"}],"version-history":[{"count":1,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/214\/revisions"}],"predecessor-version":[{"id":215,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/214\/revisions\/215"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}