1

I want to get a newly created HTML element that is toggling on and off after click event. Initially this does not show in the DOM when page loads.

I have several divs I am looping through, each with a link inside that I want to program to click and capture the new element while it is toggled. How do I go about doing this?

var div = document.getElementsByClassName('div');      // get divs
             for ( var i =0; i < div.length; i++) {    // loop through each div
    
                var link = div[i].lastChild;           // get link inside individual div

                link.click();                          // click link

  var popup = document.getElementsByClassName('popup');    // capture new div

    console.log(popup);                                    // HTMLCollection length is 0    


} 

I've put the variable y into a loop and comes as undefined. This is a live / toggle DOM event, I just don't know how to go about doing this, to get that element.

Thanks

Allison
  • 65
  • 2
  • 8
  • You can always try to use [event delegation pattern](https://javascript.info/event-delegation#:~:text=Introduction%20to%20Events-,Event%20delegation,handler%20on%20their%20common%20ancestor.) and avoid creating new `onclick` events in order to optimize memory usage – Lhew Jul 22 '20 at 08:54

0 Answers0