{"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=IQoJb3JpZ2luX2VjELX%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCWV1LXdlc3QtMSJHMEUCIFBHVE0sB1JLoKOoooEKmTy41XjQv7jfqILXhUeKN9VLAiEA0kWfUvfmTsCoVe3fR8KxNi%2BgPEhQL2yFA9zmEvoYaKIquQUIfhAFGgwyNzM1NzAxOTU0MzMiDEJj8LUt1OSbA%2BRyxyqWBZruCU1sbP%2BkYlS2k5RkgqGU9ijP%2Fa6hWxVT3jLq5Chq30LTkKS6V0ypPKgjoGzVBYlqgYhW0sD%2FZ4q8Sz2mE9gk7gZrEledrBdmCRBtxqr16SdFrgBRCZJJ%2FHmIitWfn%2FotRYxcPjwUBRfCECNCYnl3iBxXrRjYSVe9BxXu1we5XpFNjh3Gvf%2BqeG22oo4PNT86ncSnBewAVN4iaUd8zhv8DvXCHq%2FtVERfdm94JrFmhC%2BP6%2Br36SRscfKemryJLUsrRCgDQus8Dk7F3m1gC3URuYLBzhHikk6d2DuZQhf2%2FtNs8S3P7gi7p8mdQiKyPZCbHqSHDemQ4saiLkyPnBbVyVv5Q6y5o1birD%2BZOT7o5iWgrB93BTfbUW5oZdCdxlABg0dBTavuo0uSZ8ST6uiTmH%2BOWz33I0zvalOKCoF7EHh3aH%2FvsBp9dfkjVXOEQXRvAuGJagzA4J7AQ1DI%2BUiWYp3w3pXHX57kvEy9jjgm%2BVl%2BN8QOEqWZ1KE8XQrmdpBu2Nas4oMDb2XrjXHZOk7tMRq00A1xXyi4PIoYYb6LR5muuxfu5KsIRJ1q4npIGcs3JJ%2BqeNKsKVxXqhBpQ4kvn4oJmN%2F9L4Rmp4RBQ%2BtCpS1kFZRRJ551nWrR7Q7lmNUFNP78Lft5MYEnt478E9pP%2FNGfzckgCRct0aubJFY5STanJfqMos%2F4GeNOdVJa03LBO6Ts%2BNzI2%2ByuyetDYSuaw6OZFdsMszHauRea5AxtiLwV%2FF81ZORXEx22kIvovN5jNgAyx%2FEiRLEpDc4BPqkHi9Ut06%2BHwtjq9owis4YSWGrWzuKpvvkrtI1oqy3WmDXOnyUHYoWf0aIV9gA0VLU6QOrq2hZMHpPmbQPafrBtC50EdqUVMOyz%2BdUGOrEBl7Wa%2FNYNxZTeCpMyhUPpVAb6oPwYVmyPxgrMgnZgLA1vqQ6ktpH5RRN%2BmRQZoL7hKYxaiovEzDsbbakrwgqkC%2FkOHLWPxBrHOY1qZLgxq7NFk443tFEVV0ZhgpZyNsyamThcksD51%2F0vR8%2Bth69TSXLiHs7EsoYgTDKLHMd%2By5pUKFg1KitGCkbBy%2B3ZEtaW9bI%2Bk2S6%2BrLF9yWCboxul22NwhYL4459PQiYy8MlJH5V&#038;X-Amz-Algorithm=AWS4-HMAC-SHA256&#038;X-Amz-Credential=ASIAT7MQN47U72AAQYAE%2F20261001%2Feu-west-1%2Fs3%2Faws4_request&#038;X-Amz-Date=20261001T134303Z&#038;X-Amz-SignedHeaders=host&#038;X-Amz-Expires=900&#038;X-Amz-Signature=7482e24e880b719f5cc90230af008030dbc03ce961bdb3ad244798d901d597c4\" 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=IQoJb3JpZ2luX2VjELX%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCWV1LXdlc3QtMSJHMEUCIFBHVE0sB1JLoKOoooEKmTy41XjQv7jfqILXhUeKN9VLAiEA0kWfUvfmTsCoVe3fR8KxNi%2BgPEhQL2yFA9zmEvoYaKIquQUIfhAFGgwyNzM1NzAxOTU0MzMiDEJj8LUt1OSbA%2BRyxyqWBZruCU1sbP%2BkYlS2k5RkgqGU9ijP%2Fa6hWxVT3jLq5Chq30LTkKS6V0ypPKgjoGzVBYlqgYhW0sD%2FZ4q8Sz2mE9gk7gZrEledrBdmCRBtxqr16SdFrgBRCZJJ%2FHm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&#038;X-Amz-Algorithm=AWS4-HMAC-SHA256&#038;X-Amz-Credential=ASIAT7MQN47U72AAQYAE%2F20261001%2Feu-west-1%2Fs3%2Faws4_request&#038;X-Amz-Date=20261001T134303Z&#038;X-Amz-SignedHeaders=host&#038;X-Amz-Expires=900&#038;X-Amz-Signature=930105f374fe272112be4f596bc73d2aa145b0796e6740fa6ed6818d62ddb332\" 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}]}}