{"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=IQoJb3JpZ2luX2VjEK%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCWV1LXdlc3QtMSJIMEYCIQC0Rle7lLfae7oHUSTXlTYzV%2BDbdi8e6rx0rgK35JsPKQIhAK%2BfxguiN22mc8WEud60IJnk97LDt1wdo7Ahrdv0kGksKrgFCHgQBRoMMjczNTcwMTk1NDMzIgxAoQyfLe8yhbYpPSUqlQX6F1hlLi0CvIsAX8Rxln3JmxRqp%2Bpr2Q%2FW81qv5nevok9%2BlUSwIJzgyu%2FrgH6%2BV1ZyFj8VLrvS909ykJ%2Ftvyzxv%2FbICKUeQmMd1RyuE8hRVnohk%2B1Mqeu6%2BzjKThVvqJ%2BQA%2F1eQ52CIDw1OJM1KaWSGa6umGpK1AWuL1ARXAxcmwpOOpuc40bxs5hV9eK0p31e%2FL%2F2rNENA%2BQRkb7l56OgTH1ufkl3sJaxwcEbKFxkhWUQvJrH7fWNqtcgJBcD5vmpSX0Vtum9d2AXQzpUN18iEl9CXZwwQ50OavHR42%2BnI2CEkMQTPwWsMqgt4i%2Fbi36AXTbD1rToM6uffnTVulnmSZwk68T5MgDaNccwibLHHRz2OI9%2FM9NbbH9%2FAFTfc2w2rCZYUEyLE3K6PEQ4lFaP5z8ZNEsLolx%2FlaINy%2FhYwrbYTktej1W3GS74%2FGwx9qwiytVNtAFy3N8s1gNR7XFxpLbFFnlQfhTd%2BeiBGmT3VK785teVUQ3oR2iKfn2kH6%2BX4f0g4GLumlcBdRoOwceRxyj4ss%2F2uPBrkJeXaRQpC71YrZvxPySc1JeAme6f0OcErPC9aqMpIQIR3voEFQ7PivuW28Wbi6y76t7Cpa3M7T2M45EGsUnKxidZQ%2B71uqiWZYqOaQ4AM5ed%2BcnOAvA1%2FShVuYj59Zz5VHoJ8PfZwKA7KBbaemBejfF1ShO3%2FcN24tEfTrwbLXFFCjNCk6jXRhV27IPUrwsliHCvIE2YnRIFH8SVKAiMLb0gp%2B4CjOUI0xDWvqehxpzGOUhZdBHxuDYk5TWSDPsfX%2F1XavBH6b1SFLK0KXLZqi281GsAuabko5%2B%2FBwO32yGn2uq1YHNQMulTnaMwyhhBZfHU3e%2Bup3OUlJGoMITA6NYGOrABI8vkxlAhh1L3sIijYlb%2Bw6A4OgdG2KU8dlRFAevVH4Pe85907e99P7g5Gtx27DIeQFyqdFPsM0jA53LBsAQKL7DGmWdvQpmx2PghfbQzCoqyK8Y%2Fr%2BzkK36iAk0HZFOyCsCr6Pz7r5CWf4UbBekRGE%2FbY25zvep8dSzkIfroybGxJVr300uxf1f3svW2HUi3v86q4xMDEdtXsotkJ%2B%2Bc%2B8c1AswJb7KANn4%2FWYs5cD0%3D&#038;X-Amz-Algorithm=AWS4-HMAC-SHA256&#038;X-Amz-Credential=ASIAT7MQN47UZJTJ5VGO%2F20261022%2Feu-west-1%2Fs3%2Faws4_request&#038;X-Amz-Date=20261022T153837Z&#038;X-Amz-SignedHeaders=host&#038;X-Amz-Expires=900&#038;X-Amz-Signature=b3863f255d75eab91f6ef45ddf7afe1a1622a05dab9e8f42c3effdced218d075\" 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=IQoJb3JpZ2luX2VjEK%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCWV1LXdlc3QtMSJIMEYCIQC0Rle7lLfae7oHUSTXlTYzV%2BDbdi8e6rx0rgK35JsPKQIhAK%2BfxguiN22mc8WEud60IJnk97LDt1wdo7Ahrdv0kGksKrgFCHgQBRoMMjczNTcwMTk1NDMzIgxAoQyfLe8yhbYpPSUqlQX6F1hlLi0CvIsAX8Rxln3JmxRqp%2Bpr2Q%2FW81qv5nevok9%2BlUSwIJzgyu%2FrgH6%2BV1ZyFj8VLrvS909ykJ%2Ftvyzxv%2FbICKUeQmMd1RyuE8hRV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2FWYs5cD0%3D&#038;X-Amz-Algorithm=AWS4-HMAC-SHA256&#038;X-Amz-Credential=ASIAT7MQN47UZJTJ5VGO%2F20261022%2Feu-west-1%2Fs3%2Faws4_request&#038;X-Amz-Date=20261022T153837Z&#038;X-Amz-SignedHeaders=host&#038;X-Amz-Expires=900&#038;X-Amz-Signature=f1801717805446cc171df84d8731af7744f68ae69d1392feb3b7f15480f1fe1d\" 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}]}}