{"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 loading=\"lazy\" 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=IQoJb3JpZ2luX2VjEMT%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCWV1LXdlc3QtMSJGMEQCIGugESV4wLRedzh9FQKCWjMO6n2NOhM5q6UyVt4d9i2GAiAjwoZXcjf%2BtU5KD15qnWf8mOZGcSkcD9YUtL4xhk6CKCrCBQiN%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F8BEAUaDDI3MzU3MDE5NTQzMyIMkVYMvNfs9GfecU80KpYF9V9sibbzrJL%2BKJieMEfCXz3cmP9Ge54Ul4rBu%2BgOvBqAv1hSXN9sHlKjGxclpD0HHxsMHmUrrh3j0UQSor2WwCQrz7%2FUlLXoBY0gWVkWs8PtZS%2BdMaY9hTaBrkdQIqE363LgEDJhKV1W5yMAlxe5E0L9byDAskoRvxHSqfQLuIZn16D68fxL35PyHLkM4HIO1yufHLvIDwIK6Ju79fcfCg6nSyPXUuFdk9bvcQRtLcOB1iuvvBmNxjQ5X2g9FBokoaX5mDjWjnHY9wL7knMBMRyhrI0JgKZ4l%2F3MdyyRaceTjZOEEap08B%2Fsyu6twOUAzTxbxBMdQRtEwP0XBcTJblCtl2FTfY6iu3%2FckIoCpJAoQ7ntN45j7b%2FphyJKjAiUPyZu5FNeF0RvF6hEWlFFUlLsAYVxLrguQN8vNc5Yv7Td2wR7NPvyGPJtQpwYX4hiGjjSYQKOxT8YRpQc92yzeb7KkQXwuz2BQ7Mw%2B%2F%2BpgsCQpKWPy31%2Fc4SGKXSoTJ7xKNgdnTfOGkmI4pbhcKhxfFNOmQVXnAdOL5XV5aXZSga7OG3vBzaXGDFMRxarxh2NaXwVeoSU4kudvYKwc4Wklydexs2GdpqQ%2FbxiAE9QkF2TfESsOi4ysnO5JTQ4gCqb3XmPspv%2FP1UzRVim1nFDoI4NV8jQf2f50JkXUs%2FW0Jo%2BdmV7h1cERyWYkSV6stYcJz7RMCHCbxlZKokJ5JptlBejGtyo3WcppkDGrakpFbkUf542K4Jj23tk90Y6EV2yPYp7KfCn5l29LhOC8F%2B%2BxzS3bzfptYeEnb8ZXDt5e9yE0QOSTxndgDEs9MbdbaAA6rLk%2BMspCNj5W%2BwGS0YyEQM8KWvBGS%2B9k0QMyfjc2vh8h%2F5xhXIwsrGh0AY6sgGC40YFyCu3Nzm3BxIyEYLc5nwgUZ6%2Flas1CObgEVW3l2IqPv8Kk%2FzLrpgU5WIr98bMv2JVJwTTBgUrJ3kc8ZzriByNRkdwIahA1cLkjUPlfe5bOJK7Cuh%2FnAECAw8uu3yAfGEwOK7f1Cg0mHn2kxm3SsY7%2FA8TFtkurn8NLKYsM0jRRZ2CAta%2BS9aH%2B1FVREtW%2BKvypDJ9Ni6DKuofSeiE2plkVj6u%2BUevQyJ9xODuy4zi&#038;X-Amz-Algorithm=AWS4-HMAC-SHA256&#038;X-Amz-Credential=ASIAT7MQN47UVLUTULUV%2F20260516%2Feu-west-1%2Fs3%2Faws4_request&#038;X-Amz-Date=20260516T121546Z&#038;X-Amz-SignedHeaders=host&#038;X-Amz-Expires=900&#038;X-Amz-Signature=ac0f94b9863f6db29337b040103f3991d5e190d1ca26023339820c34d6b832c7\" 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 loading=\"lazy\" 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=IQoJb3JpZ2luX2VjEMT%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCWV1LXdlc3QtMSJGMEQCIGugESV4wLRedzh9FQKCWjMO6n2NOhM5q6UyVt4d9i2GAiAjwoZXcjf%2BtU5KD15qnWf8mOZGcSkcD9YUtL4xhk6CKCrCBQiN%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F8BEAUaDDI3MzU3MDE5NTQzMyIMkVYMvNfs9GfecU80KpYF9V9sibbzrJL%2BKJieMEfCXz3cmP9Ge54Ul4rBu%2BgOvBqAv1h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urn8NLKYsM0jRRZ2CAta%2BS9aH%2B1FVREtW%2BKvypDJ9Ni6DKuofSeiE2plkVj6u%2BUevQyJ9xODuy4zi&#038;X-Amz-Algorithm=AWS4-HMAC-SHA256&#038;X-Amz-Credential=ASIAT7MQN47UVLUTULUV%2F20260516%2Feu-west-1%2Fs3%2Faws4_request&#038;X-Amz-Date=20260516T121546Z&#038;X-Amz-SignedHeaders=host&#038;X-Amz-Expires=900&#038;X-Amz-Signature=d1b8b88cf07957fca7985551150619fca0c363f5be9f9f830a83db91526fa69e\" 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}]}}