The elements you end up with are all added at the last iteration, that's why eventlistener works for the last element. So it keeps looping the page.

Each location is added with JavaScript as well as it’s corresponding content and InfoWindow. When you hover over the map marker, the InfoWindow shows above the marker with some helpful information about the location. Questions and posts about frontend development in general are welcome, as are all posts pertaining to JavaScript on the backend. The type of the event object depends on the specified event. ._3Im6OD67aKo33nql4FpSp_{border:1px solid var(--newCommunityTheme-widgetColors-sidebarWidgetBorderColor);border-radius:5px 5px 4px 4px;overflow:visible;word-wrap:break-word;background-color:var(--newCommunityTheme-body);padding:12px}.lnK0-OzG7nLFydTWuXGcY{font-size:10px;font-weight:700;letter-spacing:.5px;line-height:12px;text-transform:uppercase;padding-bottom:4px;color:var(--newCommunityTheme-navIcon)} You can add an event listener to all the elements returned by a document.querySelectorAll() call by iterating over those results using the for..of loop: const buttons = document. For example, the "click" event belongs to the MouseEvent object. The custom alertButton() function is the callback function that will be called when the user clicks the button. I am looking forward to hearing from you and Happy Coding! Then, pass the global variable i into the closure with the final set of ()’s, which calls the closure once on each iteration. useCapture: Optional. This proves that the variable i is globally scoped.

Can I access the remote window's onload event from the opener window?

This works wonderfully, thank you so much! This is one of the common problems developers face when they start learning JavaScript. I also attached a click event to a button element and appended it to the buttonContainer element on each iteration. The below example is a simple map that receives four locations from a JavaScript array. ._3Qx5bBCG_O8wVZee9J-KyJ{border-top:1px solid var(--newRedditTheme-line);margin-top:16px;padding-top:16px}._3Qx5bBCG_O8wVZee9J-KyJ ._2NbKFI9n3wPM76pgfAPEsN{margin:0;padding:0}._3Qx5bBCG_O8wVZee9J-KyJ ._2NbKFI9n3wPM76pgfAPEsN ._2btz68cXFBI3RWcfSNwbmJ{font-family:Noto Sans,Arial,sans-serif;font-size:14px;font-weight:400;line-height:21px;display:-ms-flexbox;display:flex;-ms-flex-pack:justify;justify-content:space-between;margin:8px 0}._3Qx5bBCG_O8wVZee9J-KyJ ._2NbKFI9n3wPM76pgfAPEsN ._2btz68cXFBI3RWcfSNwbmJ.QgBK4ECuqpeR2umRjYcP2{opacity:.4}._3Qx5bBCG_O8wVZee9J-KyJ ._2NbKFI9n3wPM76pgfAPEsN ._2btz68cXFBI3RWcfSNwbmJ label{font-size:12px;font-weight:500;line-height:16px;display:-ms-flexbox;display:flex;-ms-flex-align:center;align-items:center}._3Qx5bBCG_O8wVZee9J-KyJ ._2NbKFI9n3wPM76pgfAPEsN ._2btz68cXFBI3RWcfSNwbmJ label svg{fill:currentColor;height:20px;margin-right:4px;width:20px}._3Qx5bBCG_O8wVZee9J-KyJ ._4OtOUaGIjjp2cNJMUxme_{-ms-flex-align:center;align-items:center;display:-ms-flexbox;display:flex;-ms-flex-pack:justify;justify-content:space-between;padding:0;width:100%}._3Qx5bBCG_O8wVZee9J-KyJ ._4OtOUaGIjjp2cNJMUxme_ svg{display:inline-block;height:12px;width:12px}._1ra1vBLrjtHjhYDZ_gOy8F{--textColor:var(--newCommunityTheme-widgetColors-sidebarWidgetTextColor);--textColorHover:var(--newCommunityTheme-widgetColors-sidebarWidgetTextColorShaded80);font-size:10px;font-weight:700;letter-spacing:.5px;line-height:12px;text-transform:uppercase;color:var(--textColor);fill:var(--textColor);opacity:1}._1ra1vBLrjtHjhYDZ_gOy8F._2UlgIO1LIFVpT30ItAtPfb{--textColor:var(--newRedditTheme-widgetColors-sidebarWidgetTextColor);--textColorHover:var(--newRedditTheme-widgetColors-sidebarWidgetTextColorShaded80)}._1ra1vBLrjtHjhYDZ_gOy8F:active,._1ra1vBLrjtHjhYDZ_gOy8F:hover{color:var(--textColorHover);fill:var(--textColorHover)}._1ra1vBLrjtHjhYDZ_gOy8F:disabled,._1ra1vBLrjtHjhYDZ_gOy8F[data-disabled],._1ra1vBLrjtHjhYDZ_gOy8F[disabled]{opacity:.5;cursor:not-allowed}

Because of each innerHTML recreates the elements, all of your previous event listeners are lost. ._3-SW6hQX6gXK9G4FM74obr{display:inline-block;vertical-align:text-bottom;width:16px;height:16px;font-size:16px;line-height:16px} By default, a variable declared with var keyword is function scoped but not block-scoped. Any help would be greatly appreciated. ._2a172ppKObqWfRHr8eWBKV{-ms-flex-negative:0;flex-shrink:0;margin-right:8px}._39-woRduNuowN7G4JTW4I8{border-top:1px solid var(--newCommunityTheme-widgetColors-lineColor);margin-top:12px;padding-top:12px}._3AOoBdXa2QKVKqIEmG7Vkb{font-size:12px;font-weight:400;line-height:16px;-ms-flex-align:center;align-items:center;background-color:var(--newCommunityTheme-body);border-radius:4px;display:-ms-flexbox;display:flex;-ms-flex-direction:row;flex-direction:row;margin-top:12px}.vzEDg-tM8ZDpEfJnbaJuU{color:var(--newCommunityTheme-button);fill:var(--newCommunityTheme-button);height:14px;width:14px}.r51dfG6q3N-4exmkjHQg_{font-size:10px;font-weight:700;letter-spacing:.5px;line-height:12px;text-transform:uppercase;display:-ms-flexbox;display:flex;-ms-flex-pack:justify;justify-content:space-between}._2ygXHcy_x6RG74BMk0UKkN{margin-left:8px}._2BnLYNBALzjH6p_ollJ-RF{display:-ms-flexbox;display:flex;margin-left:auto}._1-25VxiIsZFVU88qFh-T8p{padding:0}._3BmRwhm18nr4GmDhkoSgtb{color:var(--newCommunityTheme-bodyText);-ms-flex:0 0 auto;flex:0 0 auto;line-height:16px} If there is no listener, the event is lost. JavaScript For Loop Click Event ← Issues & Solutions Explained, Vue JS2 + Google Maps API: Build 3 Location-Based Web Apps [Firebase], Build Web Apps with Vue JS 2 & Firebase, Vue.js: Full Stack App With Firebase, Vuex & Router [2020], Quasar Framework: Cross-Platform Vue JS Vuex & Firebase Apps, Vue JS 2 - The Complete Guide (incl. In closure, the inner functions can have access to variables that are declared outside of it even after the outer function is returned. .s5ap8yh1b4ZfwxvHizW3f{color:var(--newCommunityTheme-metaText);padding-top:5px}.s5ap8yh1b4ZfwxvHizW3f._19JhaP1slDQqu2XgT3vVS0{color:#ea0027}