{"id":925,"date":"2022-06-24T03:52:26","date_gmt":"2022-06-24T02:52:26","guid":{"rendered":"https:\/\/codeflarelimited.com\/blog\/?p=925"},"modified":"2022-06-24T03:52:28","modified_gmt":"2022-06-24T02:52:28","slug":"javascript-change-background-color-on-click","status":"publish","type":"post","link":"https:\/\/codeflarelimited.com\/blog\/javascript-change-background-color-on-click\/","title":{"rendered":"JavaScript Change Background Color on Click"},"content":{"rendered":"\n<p>In this tutorial, we are going to change the background color of a webpage to the one supplied by the user in the input field.<\/p>\n\n\n\n<p>Let&#8217;s get Started!<\/p>\n\n\n\n<pre title=\"index.html\" class=\"wp-block-code\"><code lang=\"markup\" class=\"language-markup\">&lt;!DOCTYPE html>\n&lt;html lang=\"en\" dir=\"ltr\">\n  &lt;head>\n    &lt;meta charset=\"utf-8\">\n    &lt;title>Change JavaScript Background Color on Click&lt;\/title>\n    &lt;link href=\"https:\/\/cdn.jsdelivr.net\/npm\/bootstrap@5.2.0-beta1\/dist\/css\/bootstrap.min.css\" rel=\"stylesheet\" integrity=\"sha384-0evHe\/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor\" crossorigin=\"anonymous\">\n    &lt;style>\n    .colorContainer {\n      display:flex;\n      flex-direction: row;\n      justify-content: center;\n      align-items: center;\n    }\n    &lt;\/style>\n  &lt;\/head>\n  &lt;body>\n    &lt;div class=\"container d-flex justify-content-center colorContainer\">\n    &lt;form>\n    &lt;input id=\"colorName\" type=\"text\" class=\"form-control-lg\" placeholder=\"Enter color name or code\" \/>\n    &lt;button type=\"button\" id=\"btn\" class=\"btn btn-primary btn-lg\" onclick=\"changeColor()\">Change Color&lt;\/button>\n    &lt;\/form>\n    &lt;\/div>\n\n\n    &lt;script>\n   let colorName = document.getElementById(\"colorName\");\n   let btn = document.getElementById(\"btn\");\n\n   function changeColor() {\n     document.body.style.backgroundColor = colorName.value;\n   }\n\n    &lt;\/script>\n\n  &lt;\/body>\n&lt;\/html>\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-result\">Result:<\/h2>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"900\" style=\"aspect-ratio: 1440 \/ 900;\" width=\"1440\" controls loop src=\"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2022\/06\/Screen-Recording-2022-06-24-at-03.06.27.mov\"><\/video><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-see-also\">See also<\/h3>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-css-dropdown-menu\"><a href=\"https:\/\/codeflarelimited.com\/blog\/css-dropdown-menu\/\" target=\"_blank\" rel=\"noreferrer noopener\">CSS Dropdown Menu<\/a><\/h2>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we are going to change the background color of a webpage to the one supplied<\/p>\n","protected":false},"author":1,"featured_media":927,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[32],"tags":[],"class_list":["post-925","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cascading-style-sheet"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>JavaScript Change Background Color on Click<\/title>\n<meta name=\"description\" content=\"In this tutorial, we are going to change the background color of a webpage to the one supplied by the user in the input field.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codeflarelimited.com\/blog\/javascript-change-background-color-on-click\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Change Background Color on Click\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, we are going to change the background color of a webpage to the one supplied by the user in the input field.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codeflarelimited.com\/blog\/javascript-change-background-color-on-click\/\" \/>\n<meta property=\"article:author\" content=\"https:\/\/facebook.com\/codeflretech\" \/>\n<meta property=\"article:published_time\" content=\"2022-06-24T02:52:26+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-06-24T02:52:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2022\/06\/Screenshot-2022-06-24-at-03.47.18.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1377\" \/>\n\t<meta property=\"og:image:height\" content=\"786\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"codeflare\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codeflaretech\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript-change-background-color-on-click\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript-change-background-color-on-click\\\/\"},\"author\":{\"name\":\"codeflare\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#\\\/schema\\\/person\\\/7e65653d49add95629f8c1053c5cd76a\"},\"headline\":\"JavaScript Change Background Color on Click\",\"datePublished\":\"2022-06-24T02:52:26+00:00\",\"dateModified\":\"2022-06-24T02:52:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript-change-background-color-on-click\\\/\"},\"wordCount\":40,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript-change-background-color-on-click\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/Screenshot-2022-06-24-at-03.47.18.png\",\"articleSection\":[\"Cascading style sheet\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript-change-background-color-on-click\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript-change-background-color-on-click\\\/\",\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript-change-background-color-on-click\\\/\",\"name\":\"JavaScript Change Background Color on Click\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript-change-background-color-on-click\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript-change-background-color-on-click\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/Screenshot-2022-06-24-at-03.47.18.png\",\"datePublished\":\"2022-06-24T02:52:26+00:00\",\"dateModified\":\"2022-06-24T02:52:28+00:00\",\"description\":\"In this tutorial, we are going to change the background color of a webpage to the one supplied by the user in the input field.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript-change-background-color-on-click\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript-change-background-color-on-click\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript-change-background-color-on-click\\\/#primaryimage\",\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/Screenshot-2022-06-24-at-03.47.18.png\",\"contentUrl\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/Screenshot-2022-06-24-at-03.47.18.png\",\"width\":1377,\"height\":786,\"caption\":\"change background color on click\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript-change-background-color-on-click\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Cascading style sheet\",\"item\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/cascading-style-sheet\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"JavaScript Change Background Color on Click\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/\",\"name\":\"\",\"description\":\"Sustainable solutions\",\"publisher\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#organization\",\"name\":\"Codeflare Limited\",\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/codeflare.png\",\"contentUrl\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/codeflare.png\",\"width\":1040,\"height\":263,\"caption\":\"Codeflare Limited\"},\"image\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#\\\/schema\\\/person\\\/7e65653d49add95629f8c1053c5cd76a\",\"name\":\"codeflare\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/59cef917c86d965eea581d2747f51bd6382003a68bfce7c8a4dfec98b4cd838d?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/59cef917c86d965eea581d2747f51bd6382003a68bfce7c8a4dfec98b4cd838d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/59cef917c86d965eea581d2747f51bd6382003a68bfce7c8a4dfec98b4cd838d?s=96&d=mm&r=g\",\"caption\":\"codeflare\"},\"description\":\"Latest tech news and coding tips.\",\"sameAs\":[\"https:\\\/\\\/codeflarelimited.com\\\/blog\",\"https:\\\/\\\/facebook.com\\\/codeflretech\",\"https:\\\/\\\/instagram.com\\\/codeflaretech\",\"https:\\\/\\\/x.com\\\/codeflaretech\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCuBLtiYqsajHdqw0uyt7Ofw?sub_confirmation=1\"],\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/author\\\/watcher\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript Change Background Color on Click","description":"In this tutorial, we are going to change the background color of a webpage to the one supplied by the user in the input field.","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:\/\/codeflarelimited.com\/blog\/javascript-change-background-color-on-click\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Change Background Color on Click","og_description":"In this tutorial, we are going to change the background color of a webpage to the one supplied by the user in the input field.","og_url":"https:\/\/codeflarelimited.com\/blog\/javascript-change-background-color-on-click\/","article_author":"https:\/\/facebook.com\/codeflretech","article_published_time":"2022-06-24T02:52:26+00:00","article_modified_time":"2022-06-24T02:52:28+00:00","og_image":[{"width":1377,"height":786,"url":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2022\/06\/Screenshot-2022-06-24-at-03.47.18.png","type":"image\/png"}],"author":"codeflare","twitter_card":"summary_large_image","twitter_creator":"@codeflaretech","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/codeflarelimited.com\/blog\/javascript-change-background-color-on-click\/#article","isPartOf":{"@id":"https:\/\/codeflarelimited.com\/blog\/javascript-change-background-color-on-click\/"},"author":{"name":"codeflare","@id":"https:\/\/codeflarelimited.com\/blog\/#\/schema\/person\/7e65653d49add95629f8c1053c5cd76a"},"headline":"JavaScript Change Background Color on Click","datePublished":"2022-06-24T02:52:26+00:00","dateModified":"2022-06-24T02:52:28+00:00","mainEntityOfPage":{"@id":"https:\/\/codeflarelimited.com\/blog\/javascript-change-background-color-on-click\/"},"wordCount":40,"commentCount":0,"publisher":{"@id":"https:\/\/codeflarelimited.com\/blog\/#organization"},"image":{"@id":"https:\/\/codeflarelimited.com\/blog\/javascript-change-background-color-on-click\/#primaryimage"},"thumbnailUrl":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2022\/06\/Screenshot-2022-06-24-at-03.47.18.png","articleSection":["Cascading style sheet"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/codeflarelimited.com\/blog\/javascript-change-background-color-on-click\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/codeflarelimited.com\/blog\/javascript-change-background-color-on-click\/","url":"https:\/\/codeflarelimited.com\/blog\/javascript-change-background-color-on-click\/","name":"JavaScript Change Background Color on Click","isPartOf":{"@id":"https:\/\/codeflarelimited.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/codeflarelimited.com\/blog\/javascript-change-background-color-on-click\/#primaryimage"},"image":{"@id":"https:\/\/codeflarelimited.com\/blog\/javascript-change-background-color-on-click\/#primaryimage"},"thumbnailUrl":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2022\/06\/Screenshot-2022-06-24-at-03.47.18.png","datePublished":"2022-06-24T02:52:26+00:00","dateModified":"2022-06-24T02:52:28+00:00","description":"In this tutorial, we are going to change the background color of a webpage to the one supplied by the user in the input field.","breadcrumb":{"@id":"https:\/\/codeflarelimited.com\/blog\/javascript-change-background-color-on-click\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codeflarelimited.com\/blog\/javascript-change-background-color-on-click\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codeflarelimited.com\/blog\/javascript-change-background-color-on-click\/#primaryimage","url":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2022\/06\/Screenshot-2022-06-24-at-03.47.18.png","contentUrl":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2022\/06\/Screenshot-2022-06-24-at-03.47.18.png","width":1377,"height":786,"caption":"change background color on click"},{"@type":"BreadcrumbList","@id":"https:\/\/codeflarelimited.com\/blog\/javascript-change-background-color-on-click\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codeflarelimited.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Cascading style sheet","item":"https:\/\/codeflarelimited.com\/blog\/cascading-style-sheet\/"},{"@type":"ListItem","position":3,"name":"JavaScript Change Background Color on Click"}]},{"@type":"WebSite","@id":"https:\/\/codeflarelimited.com\/blog\/#website","url":"https:\/\/codeflarelimited.com\/blog\/","name":"","description":"Sustainable solutions","publisher":{"@id":"https:\/\/codeflarelimited.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codeflarelimited.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/codeflarelimited.com\/blog\/#organization","name":"Codeflare Limited","url":"https:\/\/codeflarelimited.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codeflarelimited.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2020\/11\/codeflare.png","contentUrl":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2020\/11\/codeflare.png","width":1040,"height":263,"caption":"Codeflare Limited"},"image":{"@id":"https:\/\/codeflarelimited.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/codeflarelimited.com\/blog\/#\/schema\/person\/7e65653d49add95629f8c1053c5cd76a","name":"codeflare","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/59cef917c86d965eea581d2747f51bd6382003a68bfce7c8a4dfec98b4cd838d?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/59cef917c86d965eea581d2747f51bd6382003a68bfce7c8a4dfec98b4cd838d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/59cef917c86d965eea581d2747f51bd6382003a68bfce7c8a4dfec98b4cd838d?s=96&d=mm&r=g","caption":"codeflare"},"description":"Latest tech news and coding tips.","sameAs":["https:\/\/codeflarelimited.com\/blog","https:\/\/facebook.com\/codeflretech","https:\/\/instagram.com\/codeflaretech","https:\/\/x.com\/codeflaretech","https:\/\/www.youtube.com\/channel\/UCuBLtiYqsajHdqw0uyt7Ofw?sub_confirmation=1"],"url":"https:\/\/codeflarelimited.com\/blog\/author\/watcher\/"}]}},"jetpack_featured_media_url":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2022\/06\/Screenshot-2022-06-24-at-03.47.18.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/posts\/925","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/comments?post=925"}],"version-history":[{"count":1,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/posts\/925\/revisions"}],"predecessor-version":[{"id":928,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/posts\/925\/revisions\/928"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/media\/927"}],"wp:attachment":[{"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/media?parent=925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/categories?post=925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/tags?post=925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}