{"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=IQoJb3JpZ2luX2VjEGwaCWV1LXdlc3QtMSJIMEYCIQCJrixMSwY1HthPC3FkC8YLIH2mxmvdbUxXFi5aTc9UHwIhAIhRRjrgzLwaIUyNt238uwgSxN5nUniGmCipyEyq%2FCUcKrgFCDUQBRoMMjczNTcwMTk1NDMzIgykpPgJw4TgbVlQyXYqlQUoQT14SDFVtL%2BUe%2BaE8YYh%2FDHTQASqoKaa1tCMvstyAYFnMejz07Woaj%2FGVCoW4WVlhB1Ia80GwD9qXQVQ1algsShnfhADM9OS6tiYR8UjmuriF7TVid429sSiisemulnAFGqH%2BvM4D6lMa6WIPVu7ra%2BkEihoCKx51H%2Fica8s75e1h6izPwzjsmJf5wHaLHbpYQyyVXqKxiE%2FrgeU2uagEQ0eKXE%2FLLXAQna9jTzMONc0hAAc2XSk8g5gy4hkezyRjxKgs%2BZEldtHnJ0ce9kvdMH4F19GBHXkU0cPasCf3bWsOq%2B2bEbKyZ04v7TTFWdJmjZCb%2BCm40rHtVywTg2qhyFXta4lx3Q37tiIDN7%2BSRKFQN5ba4z50T%2FcoRjBIDv%2BpYkjHLcdjGMWBWm6P6zTREZyXTX7AV6D1hNiCrJ0p7o1AjelB6wSgrc89xpI8Vuxh3L3RSQmBmBcagUmZjDra2fLqHvFYMj0gJLZnA5wpf52V0IMextmd33f8kwvdS%2F3M%2Fa%2B4Okhbe%2FKgyawjHFGVBHZ06YcDq2gILVLzUURspbXxG2AchvxOCKAnbZJgJ6%2B6Nv%2BFtQ5Ab9moeBqM8wwlO6tapsNvN2mqS%2B%2FDCHc%2BetratnlCEwZq3qJZq1sKNMndxLY4cGwtmpHwCM0oktXZLF7%2BYMcyH3oF%2BLOtd8rfiRJruaxVifb5cgglrb9uWGeRhSCsUOXyYgUgKrabLLvi6kqwBFabx6irg8Ds1kGNcW6LIlA2kRxZYuMKBuI9ngkvkogc3TMTRT3Iw7VlvAK4OjCAHK%2FduVAuOq6IsWUSZZ%2FEwP4MVbyA5oGtW17qQ3%2F%2BeB%2Bgn5u5Pgsw1I3EcoFGRBSGtnphCnVeHaoPgvWLZgJy7vgMK7wttEGOrABniJNTWTlRlgUNF18ULIZjZjuZN0Qv7d%2BLddGUHnTf%2FUiNRyfCZGZzxs%2FxNsavXg%2B85wEaRmkAwCw57UOr06gfCr1AggmseTzIM14qN89zRZsqwkRT19xz9g%2FhdN6UtrXRjvcghHqRTutnuDwIqJJrNAEH7S3oF9yXzkcUuqf2L6MPdU503m1b3RsPgWreY9b9TuUfBwSJDDhmrep%2BShGk2DEFlym4Tojn4d%2ByP02xYY%3D&#038;X-Amz-Algorithm=AWS4-HMAC-SHA256&#038;X-Amz-Credential=ASIAT7MQN47U2D5FP436%2F20260613%2Feu-west-1%2Fs3%2Faws4_request&#038;X-Amz-Date=20260613T201907Z&#038;X-Amz-SignedHeaders=host&#038;X-Amz-Expires=900&#038;X-Amz-Signature=123bdec98cdfa3f62ca5f6b9051f8ec2a3cd3dbbc82ce5bd1bdcc989d094c2fa\" 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=IQoJb3JpZ2luX2VjEGwaCWV1LXdlc3QtMSJIMEYCIQCJrixMSwY1HthPC3FkC8YLIH2mxmvdbUxXFi5aTc9UHwIhAIhRRjrgzLwaIUyNt238uwgSxN5nUniGmCipyEyq%2FCUcKrgFCDUQBRoMMjczNTcwMTk1NDMzIgykpPgJw4TgbVlQyXYqlQUoQT14SDFVtL%2BUe%2BaE8YYh%2FDHTQASqoKaa1tCMvstyAYFnMejz07Woaj%2FGVCoW4WVlhB1Ia80GwD9qXQVQ1algsShnfhADM9OS6tiYR8UjmuriF7TVid429sSiisemulnAFGqH%2BvM4D6lMa6WIPVu7ra%2BkEihoCKx51H%2Fica8s75e1h6izPwzjsmJf5w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%2Feu-west-1%2Fs3%2Faws4_request&#038;X-Amz-Date=20260613T201907Z&#038;X-Amz-SignedHeaders=host&#038;X-Amz-Expires=900&#038;X-Amz-Signature=773e5572df6a2528471ce85445360693bfc683de14f8cb7aab3219ba65dd408e\" 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}]}}