{"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=IQoJb3JpZ2luX2VjEMn%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCWV1LXdlc3QtMSJHMEUCIEQr1u1miaFysQf9mo%2FN3TPQ34Kug2%2BXZMaonIEBlx4DAiEAoqGjmNHf7Bmj2LtOn0Cg57nwgOdWELlAWlxdKIv3zjQqwQUIkv%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FARAFGgwyNzM1NzAxOTU0MzMiDLsLMfKa%2FFmj1cj8RiqVBdLuzqjh%2B5ZAfhVqD%2Bd%2F9MsrFPtXhha4GeKf1xIaipraPIpDjmP1OJ5WjbOB%2F7ri%2B5eaGdGN2sfQ%2B09d0zn7S7gnDEf0Mu7UK1zyqLMxP%2F0P3cK0zTG3ZL0PMe01glgZiZWjUP29WoWWHHW9RQItLhwlP4CVEjol20zqH4cUyPrefcdg%2FQ4kCFgz06qoRox5QO8%2BiVkASSc0zdHtqMwyHjb%2FJuZ%2FQZYVzpGxfDKN1u77SVKTaJoYxdh%2BqcIAc%2BqbSc52383OTw%2BIxDY4kFwo%2FL%2F58DQISMqUf9S4qeVBAhx31uUbzGEuC5QuF09r%2FmGnxzg1ufhZfH3tmGTmclBC6rkIS6uDNkaYyojac8YcpHNPY2Y%2FZ0oQwAfWk86K6ncHyP2BHOMx2pcRjovC8eHf1qfP9OOZbF039PTGvVYDmT4qbkYh9Au4US%2BKtDc3YrFQifsyX4ou%2Bh6mE9v9pXT8ufN%2FFTMxdGpbc25BlAgLN4N167jdYmPwbjF7XKbfktw4iYFmyro3RFWb%2BftkyUL2XnYixcS1iLYTl5IMAeQzWhGYrAGBcpwEkbgNlsTG0SllwZPCBBAOZfaHHcuiBioMD2bd6n3wp1uU%2B2rsuWkOdDBsYy%2F1oBLZhhQW5bag8h1Wid800LohzNio3lOL94i81L53rjv%2FKQ%2BxK19IW2hsQS5SWMOFRqdOdZuxV6XLhuvjIMeshDEZz4uJvh7x4YuUoibcU1HxYCpM8l%2FxucWXYVVHn1r0JA4KrqLbW3ITikg%2BUYzt1V5EroRCKDH0%2BSut4uREBGEbTowWuYxLPUqkJ0Uwk9A4fhLSZDiLJlhFXqdujyPPpJ%2BI7QOdGu9Dla79mnvfgrBvbJP4izWG0Z3ewbxNZj4CBjUwwNWc1AY6sQEkAwE%2F88wWpAMnVj6FM50YY0%2Blr2O9sf6Ls6eCRPx4zOhslZ7sEIMi7nFU7CUI8ejV7Hlid%2BmklJaE4sW535BKs47DDcvzwhVRnDchx5mPgz9bsiHpaSOfOeYWU2UdqSGtVe6XFI%2FvHUIbUhrufp1Ur5Yzxng0WpjZRzo95Vikc8zdzlgl0govD852gIqGirbZZMcrju9qIIGYIoCtJDTjnq%2FcHtXgll4T7SVO65kl4ok%3D&#038;X-Amz-Algorithm=AWS4-HMAC-SHA256&#038;X-Amz-Credential=ASIAT7MQN47UZL2K2M3D%2F20260820%2Feu-west-1%2Fs3%2Faws4_request&#038;X-Amz-Date=20260820T164911Z&#038;X-Amz-SignedHeaders=host&#038;X-Amz-Expires=900&#038;X-Amz-Signature=7a188be945c4ac4808b3dbe1ef983db2a44e4061eceeb111435e4c2925374933\" 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=IQoJb3JpZ2luX2VjEMn%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCWV1LXdlc3QtMSJHMEUCIEQr1u1miaFysQf9mo%2FN3TPQ34Kug2%2BXZMaonIEBlx4DAiEAoqGjmNHf7Bmj2LtOn0Cg57nwgOdWELlAWlxdKIv3zjQqwQUIkv%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FARAFGgwyNzM1NzAxOTU0MzMiDLsLMfKa%2FFmj1cj8RiqVBdLuzqjh%2B5ZAfhVqD%2Bd%2F9MsrFPtXhha4GeKf1xIaipraPIpDjmP1OJ5WjbOB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govD852gIqGirbZZMcrju9qIIGYIoCtJDTjnq%2FcHtXgll4T7SVO65kl4ok%3D&#038;X-Amz-Algorithm=AWS4-HMAC-SHA256&#038;X-Amz-Credential=ASIAT7MQN47UZL2K2M3D%2F20260820%2Feu-west-1%2Fs3%2Faws4_request&#038;X-Amz-Date=20260820T164911Z&#038;X-Amz-SignedHeaders=host&#038;X-Amz-Expires=900&#038;X-Amz-Signature=2bd88f407d1918bf6b29a053cd09a1ead8404232ecab7beb4ca32becb6cd727a\" 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}]}}