{"id":56222,"date":"2022-06-24T03:01:56","date_gmt":"2022-06-23T17:01:56","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=56222"},"modified":"2022-06-24T22:08:54","modified_gmt":"2022-06-24T12:08:54","slug":"multiple-addeventlistener-logics-for-html-elements-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/multiple-addeventlistener-logics-for-html-elements-primer-tutorial\/","title":{"rendered":"Multiple addEventListener Logics for HTML Elements Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/ael_multi.html\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"Multiple addEventListener Logics for HTML Elements Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/ael_multi.jpg\" title=\"Multiple addEventListener Logics for HTML Elements Primer Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">Multiple addEventListener Logics for HTML Elements Primer Tutorial<\/p><\/div>\n<p>The dynamic creation of event logic associated with webpage HTML elements is a powerful tool for making your webpages dynamic.<\/p>\n<p>In the case of the great <font size=1>[element].<\/font><a target=_blank title='HTML DOM Element addEventListener() information from W3schools' href='https:\/\/www.w3schools.com\/jsref\/met_element_addeventlistener.asp'>addEventListener<\/a> means by which you can make this happen, there is a great feature that goes &#8230;<\/p>\n<blockquote><p>\nIf you can swing it that the second &#8220;function&#8221; parameter to <font size=1>[element].<\/font>addEventListener is unique and just a straight simple function object reference you can pile on multiple same-type event logics onto the one HTML element\n<\/p><\/blockquote>\n<p>We wanted to combine that premise with a dynamic user entered &#8220;data item&#8221; into that mix, in our <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/ael_multi.html-GETME\" title=\"ael_multi.html\">&#8220;proof of concept&#8221; ael_multi.html<\/a>&#8216;s <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/ael_multi.html\" title=\"Click picture\">cell multiple background colour controller<\/a> web application for you, today.  This means creating a discrete &#8220;not existing beforehand&#8221; Javascript function on the fly.  Yes, <font color=blue>we can do that<\/font> via &#8230;<\/p>\n<p><code><br \/>\n   var thirty=1;<br \/>\n   var bcols=['#ffffff'];<br \/>\n   var icols=[''];<br \/>\n<br \/>\n   function moreevs() {<br \/>\n     var tag=null;<br \/>\n     for (var i=2; i<=30; i++) {\n      <font color=blue>tag = document.createElement('script');<br \/>\n      tag.type='text\/javascript';<br \/>\n      tag.innerHTML=' function ev' + i + '() { document.getElementById(icols[' + i + ']).style.backgroundColor=bcols[' + i + '];  setTimeout(ev' + i + ', 3000);  } ';<br \/>\n      document.head.appendChild(tag);<\/font><br \/>\n     }<br \/>\n     thirty=30;<br \/>\n   }<br \/>\n<br \/>\n   function ev1() {<br \/>\n      document.getElementById(icols[1]).style.backgroundColor=bcols[1];<br \/>\n      setTimeout(ev1, 3000);<br \/>\n   }<br \/>\n<\/code><\/p>\n<p> &#8230; but how do we get the proper <font size=1>[element].<\/font><a target=_blank title='HTML DOM Element addEventListener() information from W3schools' href='https:\/\/www.w3schools.com\/jsref\/met_element_addeventlistener.asp'>addEventListener<\/a> call into the mix?  This was a &#8220;first time&#8221; usage for us <font color=blue>as per the <a title=\"Javascript eval\" href=\"http:\/\/www.w3schools.com\/jsref\/jsref_eval.asp\" target=\"_blank\">eval<\/a> inspired<\/font> &#8230;<\/p>\n<p><code><br \/>\n   var fnum=0;<br \/>\n<br \/>\n   function ael_more(iois) {<br \/>\n     var tag=null;<br \/>\n     var eok=true;<br \/>\n     fnum++;<br \/>\n     if (fnum > thirty) {<br \/>\n      tag = document.createElement('script');<br \/>\n      tag.type='text\/javascript';<br \/>\n      tag.innerHTML=' function ev' + fnum + '() { document.getElementById(icols[' + fnum + ']).style.backgroundColor=bcols[' + fnum + '];  setTimeout(ev' + fnum + ', 3000);  } ';<br \/>\n      document.head.appendChild(tag);<br \/>\n      thirty=fnum;<br \/>\n      eok=false;<br \/>\n     }<br \/>\n     bcols.push(iois.value);<br \/>\n     icols.push(iois.id.replace('i','td'));<br \/>\n     if (eok) {<br \/>\n     <font color=blue>document.getElementById(iois.id.replace('i','td')).addEventListener(\"click\", eval(\"ev\" + fnum + '()'));<\/font><br \/>\n     } else {<br \/>\n     document.getElementById(iois.id.replace('i','td')).addEventListener(\"click\", window[\"ev\" + fnum + '']);<br \/>\n     }<br \/>\n     document.getElementById(iois.id.replace('i','td')).click();<br \/>\n   }<br \/>\n<\/code><\/p>\n<p> &#8230; you can see in <font size=1>(perhaps animated)<\/font> action below, where the user chooses background colours from the colour picker elements shown &#8230;<\/p>\n<p><iframe src=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/ael_multi.html\" style=\"width:100%;height:900px;\"><\/iframe><\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d56222' onclick='var dv=document.getElementById(\"d56222\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/onclick\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d56222' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The dynamic creation of event logic associated with webpage HTML elements is a powerful tool for making your webpages dynamic. In the case of the great [element].addEventListener means by which you can make this happen, there is a great feature &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/multiple-addeventlistener-logics-for-html-elements-primer-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,12,14,37],"tags":[2978,84,2682,1580,2539,3609,354,1549,399,400,471,576,652,3373,1830,861,997,1319],"class_list":["post-56222","post","type-post","status-publish","format-standard","hentry","category-animation","category-elearning","category-event-driven-programming","category-tutorials","tag-addeventlistener","tag-animation-2","tag-background-colour","tag-cell","tag-createelement","tag-document-createelement","tag-dom","tag-element","tag-eval","tag-event","tag-function","tag-html","tag-javascript","tag-logic","tag-method","tag-onclick","tag-programming","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/56222"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=56222"}],"version-history":[{"count":9,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/56222\/revisions"}],"predecessor-version":[{"id":56239,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/56222\/revisions\/56239"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=56222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=56222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=56222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}