{"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=IQoJb3JpZ2luX2VjEEUaCWV1LXdlc3QtMSJGMEQCIEQ7r3OTRX6nTB2uUaqixjnDF55d7wxAeDr7Cg2PF0GmAiBIUdwjBBKbHe9KaR61cn%2FgMl0R%2FDmQcUE2mOUkQxCO%2Fiq5BQgOEAUaDDI3MzU3MDE5NTQzMyIMw9qYEJNbHWW6RgqZKpYFH%2BpjrqgKVmDaQTOozi6b%2ByI7aY0ZTkLnya3cTClYih2aMVEpaazAeedSCJPnpnI8i33fzrUOXga%2B974zrBCa30jn%2Fd4FVWr5biU5iy%2FKcrSmr%2F%2F3y2BhP464Sm0QYSrc1g3vuyeN%2FLWqz797%2FyJXRdRUSkUuYihXugoZsGcdZPBW1y6XanCVgrbL8%2BJ9VSfklZluBtGWRMCGM%2FRTsncB3Q5ZPmKm5sJnlVnCM9sj%2BFmeLThCwK1wDF%2FcC15RMzgmIKzSy92rsxcWNmCh7w%2BEcq5GheazU4jrNXXOuOV%2FDPI7pFrWsY0QsXtYpLXpWCmfMfHo5D58JrSdD2d%2FuCdqz0Bvl1uHR6YeHU9KtIALlQEpu6bT70HLRRlIyKrqowt0dBm1kiRBSNq7etSMuHOmjJos%2FRa7Ybq5Upyxk8rC%2B0Tfe%2BTGdGtGr7DRrbZ7F480D2pteCWTiJIkkbE31Z30AYL%2FIc3Wgw7IF0xxRWIDfXZiaCA9Wdr32IXMFnqEMXBP2tjJ7%2FdA332pxjmXm9U9%2BljsHB6xQJB4wgX4UjCq9Q7nyRRTZr3A%2B%2BuIh6nsFKqoasY2mT%2BXhND7RHM15giQMx9TPQ1RJR4wgiGdd%2BJxqUZGlydt00%2Fu6K4qPCwoIdsvdZJSlgQyQamCXkCJrtY9QZnz3RcOi7j1AA87xRpb5VAazi4vXNMmWUnPyoFWQmrOQ05fZ7AMhJ%2Bk5BPz2NvQoGxd6djnKc%2B%2BRaSJAMawba3sB4Y%2FeOP2W6ur27KYP99Us3sGNAcaAE%2BheUMtzix9SQTexBfMhHOgjJGnAtFfftrrAAn88S%2F%2B%2BQChHVTSm8WGT5hnjbcM6VgC1eqRAHQ%2BnsStSSfNPKBRnOtU%2FgMrvmYbRS%2B%2FfRQw36KP0wY6sgHJQ8EjxgSFQhplGfOQ9kzTKEdSp94eTpYpe3HD6n4OWZ0X%2B%2F1LkizpPW8jtsOtaWoLBmByN8oydNgs8wWieci4EV1wdKTfOHCz0%2FsJMr6gXf3k2zeaKcsyQZ5RupljkznhDKUGn6ybxILq1XOHzfTZW6pswiLOiCFZeDvtr4L3i82WSI4LjoOyRGEuv67y3kUwknCwv3aNZ22F7yds9lHVdbLyX8%2FaUHBR6Wn%2FCb0SDNzi&#038;X-Amz-Algorithm=AWS4-HMAC-SHA256&#038;X-Amz-Credential=ASIAT7MQN47U3QOQTCYN%2F20260724%2Feu-west-1%2Fs3%2Faws4_request&#038;X-Amz-Date=20260724T210946Z&#038;X-Amz-SignedHeaders=host&#038;X-Amz-Expires=900&#038;X-Amz-Signature=4b38f349205156575514c37413d7d774d6b5d3bdff3a0825ae8f7104d0409e3a\" 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=IQoJb3JpZ2luX2VjEEUaCWV1LXdlc3QtMSJGMEQCIEQ7r3OTRX6nTB2uUaqixjnDF55d7wxAeDr7Cg2PF0GmAiBIUdwjBBKbHe9KaR61cn%2FgMl0R%2FDmQcUE2mOUkQxCO%2Fiq5BQgOEAUaDDI3MzU3MDE5NTQzMyIMw9qYEJNbHWW6RgqZKpYFH%2BpjrqgKVmDaQTOozi6b%2ByI7aY0ZTkLnya3cTClYih2aMVEpaazAeedSCJPnpnI8i33fzrUOXga%2B974zrBCa30jn%2Fd4FVWr5biU5iy%2FKcrSmr%2F%2F3y2BhP464Sm0QYSrc1g3vuyeN%2FLWqz797%2FyJXRdRUSkUuYih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-HMAC-SHA256&#038;X-Amz-Credential=ASIAT7MQN47U3QOQTCYN%2F20260724%2Feu-west-1%2Fs3%2Faws4_request&#038;X-Amz-Date=20260724T210946Z&#038;X-Amz-SignedHeaders=host&#038;X-Amz-Expires=900&#038;X-Amz-Signature=370dc3416295ea8b235a98a2872a0416c48ff90738812f968ad38f73727f010c\" 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}]}}