{"id":1245,"date":"2023-05-21T07:12:47","date_gmt":"2023-05-21T07:12:47","guid":{"rendered":"https:\/\/itnotes.apjsoftwares.com\/?p=1245"},"modified":"2023-05-21T07:12:48","modified_gmt":"2023-05-21T07:12:48","slug":"getelementsbytagname-example","status":"publish","type":"post","link":"https:\/\/itnotes.apjsoftwares.in\/index.php\/2023\/05\/21\/getelementsbytagname-example\/","title":{"rendered":"getElementsByTagName Example"},"content":{"rendered":"\n<p>getElementsByTagName: To access elements and attributes using tag name. This method will return an array of all the items with the same tag name.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;html&gt;\n\n&lt;head&gt;\n\n    &lt;title&gt;DOM!!!&lt;\/title&gt;\n\n&lt;\/head&gt;\n\n&lt;body&gt;\n\n  &lt;h3&gt;Welcome&lt;\/h3&gt;\n\n  &lt;p&gt;This is the welcome message.&lt;\/p&gt;\n\n  &lt;h3&gt;Technology&lt;\/h3&gt;\n\n  &lt;p id=\"second\"&gt;This is the technology section.&lt;\/p&gt;\n\n  &lt;script type=\"text\/javascript\"&gt;\n\n    var paragraphs = document.getElementsByTagName(\"p\");\n\n    alert(\"Content in the second paragraph is \" + paragraphs[1].innerHTML);\n\n    document.getElementById(\"second\").innerHTML = \"The orginal message is changed.\";\n\n  &lt;\/script&gt;\n\n&lt;\/body&gt;\n\n&lt;\/html&gt;\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Event handler Example<\/h4>\n\n\n\n<ol>\n<li>createElement: To create new element<\/li>\n\n\n\n<li>removeChild: Remove an element<\/li>\n\n\n\n<li>you can add an&nbsp;<strong>event handler<\/strong>&nbsp;to a particular element like this<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">    document.getElementById(id).onclick=function()\n        {\n            lines of code to be executed\n        }\n<\/pre>\n\n\n\n<p>OR<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">document.getElementById(id).addEventListener(\"click\", functionname)\n<\/pre>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;DOM!!!&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n  &lt;input type=\"button\" id=\"btnClick\" value=\"Click Me!!\" \/&gt;\n  &lt;script type=\"text\/javascript\"&gt;\n    document.getElementById(\"btnClick\").addEventListener(\"click\", clicked);\n    function clicked()\n    {\n            alert(\"You clicked me!!!\");\n    }    \n  &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>getElementsByTagName: To access elements and attributes using tag name. This method will return an array&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[23],"tags":[],"_links":{"self":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/1245"}],"collection":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/comments?post=1245"}],"version-history":[{"count":1,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/1245\/revisions"}],"predecessor-version":[{"id":1246,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/1245\/revisions\/1246"}],"wp:attachment":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/media?parent=1245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/categories?post=1245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/tags?post=1245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}