{"id":295,"date":"2021-06-08T10:26:47","date_gmt":"2021-06-08T09:26:47","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/?page_id=295"},"modified":"2021-06-08T15:46:23","modified_gmt":"2021-06-08T14:46:23","slug":"referencing-page-elements","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/javascript\/referencing-page-elements\/","title":{"rendered":"Referencing Page Elements"},"content":{"rendered":"<p><img decoding=\"async\" class=\"alignright wp-image-197\" src=\"https:\/\/glow-prod-sh.s3.eu-west-1.amazonaws.com\/sh\/public\/ahscomputinghtml\/uploads\/sites\/3275\/2021\/06\/05211153\/sqa-h-150x150.png?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&#038;X-Amz-Security-Token=IQoJb3JpZ2luX2VjEL7%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCWV1LXdlc3QtMSJHMEUCIQChMzQQMj1gm94QCYVAV20XwWhbnX6xKF7cAoo90vPLfAIgLYqtjWiDfcFvCcXGP6QKfVwzyuA6kfWK5oQSbHNa%2FR8qwQUIh%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FARAFGgwyNzM1NzAxOTU0MzMiDJp0B1pDRJotvR0jrSqVBZrYO0LmwV%2FSyPozrQ9bK6%2BcnnmgwCifdETmK0qNhJ3sP36qDxDU%2F71MNc4ndmRi1ZY5vS%2BBMQT7p1B%2F8B7XhSv5AiqWxxEEipTZ6Bcxdgn3DGKTbPDcKbk3XIEz2nSgtHyOCgjWmP2rH7%2FCUt%2BxvXB8mHWKSJgb%2F1enwcLcbzjvewPU2Yvd6EMdLiewfXgu6nn2fH5KA6nB2LNIJIPzUKcmFfMf%2BMwaQCXfC%2Bf67r7JGJfKnoZJ2zps6pTLKgtSxwMwYnrCkhI90Wb6JOfAERaROnn%2BvyXj8ws%2FX9Ug4URCLEsBUGSSW7glJIp0b9W2noKgIE%2FlJHS5NcUqCCurDV49IvnXDB9%2BVrNRv8pMt%2FMlOsH5BJCsCbEpYn2XL2AFo%2FO4xBKK7dsFcqfjNxgrNOthpmhwor6pL6zCpsdOig%2Fc3wj9zlFt%2FuXkWf2dlceWCZ2RL%2F8hjVIvJCAZhJN7%2B%2BClZmfoqgI7eXCgrj9yo8kMWRXAI6uxNN%2BAIU2fkXej6IXexlfuRQx2PQTawwSiSgVOI1al1DK9uT5Myz0g2gdQ39i0F%2Bt5fpWAMkReKFJ0Sm39fif0IbYrZ4v8rBHx1mKCPyjMITExbEDUFbl%2F%2BCtTIC0J0h507Ts2KFgxQsvDTnsbfgcWwhN896grg2J0aXuf3u1sILGq0d90hozRCcyA3F8%2BnIuq4QEBl38H%2BSQkMaPjOwoR2mMq0OsjPbqagZE3uRiRyewGTYNYVKmq2u9w6FsQEY4SAr3%2F4qsbQaUXZ33lXdYfA69WcenWlfoupTni6N%2Bd6GvxKZD9UMc5R%2BBkl3WXVch1fg6QzykEiqlCI3VTY%2BHBak%2BBKbaLF7hyTqt4vhf%2B%2BQt4dvuG1bftIX8jRzvOBQww%2BeaK1QY6sQE57cW2Gx3QPphH0V14OVcno%2BHTjRZxtvzgrhNJhetDM1d6md1y7GDwMUnZYGLxTyK1yeBeQXsydPREXipJXWINXr8zUFGh9ha8plcBivKNhTadXsrj8otDnBgiaKPsB2ve4aODn5sskJkw%2FU6xxqaqYTmoxLtpC%2F%2Bb4gnh4rh8Lvs%2FBl7A3cXafQZXiZS8Tr%2B0ePZSMOuQRRCKINxSfhZCZ5i3vsYNL77eeCwu9JKSEbw%3D&#038;X-Amz-Algorithm=AWS4-HMAC-SHA256&#038;X-Amz-Credential=ASIAT7MQN47UZN2X5VMR%2F20260910%2Feu-west-1%2Fs3%2Faws4_request&#038;X-Amz-Date=20260910T135036Z&#038;X-Amz-SignedHeaders=host&#038;X-Amz-Expires=900&#038;X-Amz-Signature=8d007f41c89acee94af1c6f06948fc25cf97041be252ea1b6a1484b7cee3d0f7\" alt=\"\" width=\"100\" height=\"97\" \/>For JavaScript functions to modify page elements, it must know how to identify them.<\/p>\n<h1>this<\/h1>\n<p><strong>this<\/strong> is a reference to the containing element of the event call.<\/p>\n<p>&#8220;this&#8221; can be passed as an actual parameter from the action event to a function&#8217;s formal parameter.<\/p>\n<p>In this example either paragraph will be modified:<\/p>\n<pre class=\"brush: jscript; title: JavaScript; notranslate\" title=\"JavaScript\">\r\nfunction highlight(element) {\r\n    element.style.backgroundColor = &quot;red&quot;;\r\n}\r\n\r\nfunction removehightlight(element) {\r\n    element.style.backgroundColor = &quot;white&quot;;\r\n}\r\n<\/pre>\n<pre class=\"brush: xml; title: HTML; notranslate\" title=\"HTML\">\r\n&lt;p onmouseover=&quot;highlight(this)&quot; onmouseout=&quot;removehightlight(this)&quot;&gt;I am special.&lt;\/p&gt;\r\n \r\n&lt;p onmouseover=&quot;highlight(this)&quot; onmouseout=&quot;removehightlight(this)&quot;&gt;So am I.&lt;\/p&gt;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h1><a href=\"https:\/\/www.w3schools.com\/js\/js_htmldom.asp\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-59 alignright\" src=\"https:\/\/glow-prod-sh.s3.eu-west-1.amazonaws.com\/sh\/public\/ahscomputinghtml\/uploads\/sites\/3275\/2021\/06\/03210120\/w3schools-150x150.png?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&#038;X-Amz-Security-Token=IQoJb3JpZ2luX2VjEL7%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCWV1LXdlc3QtMSJHMEUCIQChMzQQMj1gm94QCYVAV20XwWhbnX6xKF7cAoo90vPLfAIgLYqtjWiDfcFvCcXGP6QKfVwzyuA6kfWK5oQSbHNa%2FR8qwQUIh%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FARAFGgwyNzM1NzAxOTU0MzMiDJp0B1pDRJotvR0jrSqVBZrYO0LmwV%2FSyPozrQ9bK6%2BcnnmgwCifdETmK0qNhJ3sP36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LtpC%2F%2Bb4gnh4rh8Lvs%2FBl7A3cXafQZXiZS8Tr%2B0ePZSMOuQRRCKINxSfhZCZ5i3vsYNL77eeCwu9JKSEbw%3D&#038;X-Amz-Algorithm=AWS4-HMAC-SHA256&#038;X-Amz-Credential=ASIAT7MQN47UZN2X5VMR%2F20260910%2Feu-west-1%2Fs3%2Faws4_request&#038;X-Amz-Date=20260910T135036Z&#038;X-Amz-SignedHeaders=host&#038;X-Amz-Expires=900&#038;X-Amz-Signature=73c4ce5f872c2bad171949867ce0aec6782afe72cd8588fc271331355cc22f8e\" alt=\"\" width=\"50\" height=\"47\" \/><\/a>Document Object Model (DOM)<\/h1>\n<p><em>&#8220;The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure, and style of a document.&#8221;<\/em><\/p>\n<p>The <strong>getElementById<\/strong> method can be used to access and change the properties of any page element that has an id.<\/p>\n<p>In this example, a hidden paragraph is displayed when a button is clicked.<\/p>\n<pre class=\"brush: jscript; title: JavaScript; notranslate\" title=\"JavaScript\">\r\nfunction showMessage() {\r\n    element = document.getElementById(&quot;secretMessage&quot;)\r\n\telement.style.display=&quot;block&quot;;\r\n}\r\n<\/pre>\n<pre class=\"brush: xml; title: HTML; notranslate\" title=\"HTML\">\r\n&lt;p id=&quot;secretMessage&quot; style=&quot;display:none;&quot;&gt;This is a secret message&lt;\/p&gt;\r\n \r\n&lt;button onclick=&quot;showMessage()&quot;&gt;Show the Message&lt;\/button&gt;\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For JavaScript functions to modify page elements, it must know how to identify them. this this is a reference to the containing element of the event call. &#8220;this&#8221; can be passed as an actual parameter from the action event to a function&#8217;s formal parameter. In this example either paragraph will be modified: function highlight(element) { element.style.backgroundColor = &quot;red&quot;; } function removehightlight(element) { element.style.backgroundColor = &quot;white&quot;; } &lt;p onmouseover=&quot;highlight(this)&quot; onmouseout=&quot;removehightlight(this)&quot;&gt;I am<\/p>\n<p><a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/javascript\/referencing-page-elements\/\">Read More<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"parent":259,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-295","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/pages\/295","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/comments?post=295"}],"version-history":[{"count":9,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/pages\/295\/revisions"}],"predecessor-version":[{"id":306,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/pages\/295\/revisions\/306"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/pages\/259"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/media?parent=295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}