{"id":5139,"date":"2023-05-13T16:20:00","date_gmt":"2023-05-13T21:20:00","guid":{"rendered":"https:\/\/www.incredigeek.com\/home\/?p=5139"},"modified":"2023-05-22T21:04:59","modified_gmt":"2023-05-23T02:04:59","slug":"notes-on-buttons-with-javascript","status":"publish","type":"post","link":"https:\/\/www.incredigeek.com\/home\/notes-on-buttons-with-javascript\/","title":{"rendered":"Notes on Buttons with JavaScript"},"content":{"rendered":"\n<p>Here are some very basic notes on using buttons to change elements on a web page<\/p>\n\n\n\n<p>We have a very simple page with three buttons that change the background color when the button is clicked.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/05\/image-9.png\"><img loading=\"lazy\" decoding=\"async\" width=\"805\" height=\"289\" src=\"https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/05\/image-9.png\" alt=\"\" class=\"wp-image-5142\" srcset=\"https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/05\/image-9.png 805w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/05\/image-9-300x108.png 300w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/05\/image-9-768x276.png 768w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/05\/image-9-500x180.png 500w\" sizes=\"auto, (max-width: 805px) 100vw, 805px\" \/><\/a><\/figure>\n\n\n\n<p>Clicking a button changes the text and the background color.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/05\/image-10.png\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"289\" src=\"https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/05\/image-10.png\" alt=\"\" class=\"wp-image-5143\" srcset=\"https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/05\/image-10.png 602w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/05\/image-10-300x144.png 300w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/05\/image-10-500x240.png 500w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Create a button in our html file.<\/p>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code class=\"\">    &lt;button type=\"button\" class=\"button green\"&gt;Green&lt;\/button&gt;<\/code><\/pre>\n\n\n\n<p>We have two classes assigned to this button, button which is used for styling, and the green, which JavaScript will use to know which button is clicked.<\/p>\n\n\n\n<p>In our JavaScript, we will set up an event listener for when the button is clicked.<\/p>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code lang=\"javascript\" class=\"language-javascript line-numbers\">document.querySelector('.green').addEventListener('click', function () {\n  document.querySelector('body').style.backgroundColor = 'Green'\n  console.log('Green')\n})<\/code><\/pre>\n\n\n\n<p>We use the document.querySelector to interact with HTML objects.  So we setup the event listener to listen to the button that is in Class green, and we wait for the click event.  Once that happens, we run the code in the function(){ }.  <\/p>\n\n\n\n<p>Line 2 is what changes our background color.  We query the body tag, and set the backgroundColor to Green.  Notice that the background color name is slightly different between JavaScript and HTML.  <br>HTML and CSS it has a &#8211; in it: background-color<br>JavaScript is it Camel Case: backgroundColor<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Here is the full code for the above screenshots.  There are three files.  index.html, style.css, and index.js.  Should be able to copy them to the same folder and run the index.html file.<\/p>\n\n\n\n<p>Index.html code<\/p>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code lang=\"markup\" class=\"language-markup line-numbers\">&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n  &lt;head&gt;\n    &lt;link rel=\"stylesheet\" href=\"style.css\" \/&gt;\n    &lt;meta charset=\"UTF-8\" \/&gt;\n    &lt;meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\" \/&gt;\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" \/&gt;\n    &lt;title&gt;JavaScript DOM Practice&lt;\/title&gt;\n  &lt;\/head&gt;\n  &lt;br \/&gt;\n  &lt;section class=\"mainwindow\"&gt;\n    &lt;div class=\"output\"&gt;Click a button to change the background color&lt;\/div&gt;\n    &lt;br \/&gt;\n    &lt;br \/&gt;\n    &lt;br \/&gt;\n    &lt;button type=\"button\" class=\"button green\"&gt;Green&lt;\/button&gt;\n    &lt;button type=\"button\" class=\"button blue\"&gt;Blue&lt;\/button&gt;\n    &lt;button type=\"button\" class=\"button yellow\"&gt;Yellow&lt;\/button&gt;\n  &lt;\/section&gt;\n\n  &lt;body&gt;\n    &lt;script src=\"index.js\"&gt;&lt;\/script&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>JavaScript index.js<\/p>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code lang=\"javascript\" class=\"language-javascript line-numbers\">'use strict'\n\ndocument.querySelector('.green').addEventListener('click', function () {\n  ChangeBackgrounColor('Green')\n  LogBackgroundColor('Green')\n})\n\ndocument.querySelector('.blue').addEventListener('click', function () {\n  ChangeBackgrounColor('Blue')\n  LogBackgroundColor('Blue')\n})\n\ndocument.querySelector('.yellow').addEventListener('click', function () {\n  ChangeBackgrounColor('Yellow')\n  LogBackgroundColor('Yellow')\n})\n\nfunction ChangeBackgrounColor (color) {\n  document.querySelector('body').style.backgroundColor = color\n}\n\nfunction LogBackgroundColor (color) {\n  document.querySelector('.output').innerHTML = `Background is ${color}`\n  console.log(`Background color is ${color}`)\n}\n<\/code><\/pre>\n\n\n\n<p>CSS File style.css<\/p>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code lang=\"css\" class=\"language-css line-numbers\">.mainwindow {\n  padding: 1rem;\n}\nbody {\n  background-color: white;\n}\nbutton {\n  border: none;\n  font-size: 24px;\n  cursor: pointer;\n  padding: 1rem 1.5rem;\n  margin: 1rem;\n}\n.output {\n  font-size: 2rem;\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here are some very basic notes on using buttons to change elements on a web page We have a very simple page with three buttons that change the background color when the button is clicked. Clicking a button changes the &hellip; <a href=\"https:\/\/www.incredigeek.com\/home\/notes-on-buttons-with-javascript\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[792],"tags":[1437,1463,606],"class_list":["post-5139","post","type-post","status-publish","format-standard","hentry","category-programming","tag-javascript","tag-js","tag-programming"],"_links":{"self":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5139","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/comments?post=5139"}],"version-history":[{"count":4,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5139\/revisions"}],"predecessor-version":[{"id":5161,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5139\/revisions\/5161"}],"wp:attachment":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/media?parent=5139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/categories?post=5139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/tags?post=5139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}