Getting NULL when trying to use queryselector to fetch dom elements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dreamtext
    Recognized Expert New Member
    • Feb 2021
    • 24

    Getting NULL when trying to use queryselector to fetch dom elements

    I keeping getting NULL when using queryselector to fetch DOMelements that have been created after the site has loaded.

    Does anyone know why this happens?

    The first selector clicks on the menu
    The second selector clicks on the menu option that only loads and gets added to the DOM after the first is clicked

    1) site loads
    2) i run the following code to fetch dom elements via the web developer console
    Code:
    function fetchElement(){
    	document.querySelectorAll('[data-menu="menu"]')[0].click()
    	document.querySelectorAll('[data-option="menuitem"]')[0].click()
    }
    fetchElement()
    The second click returns null.

    I am wondering if it's because those menuitems get added to the DOM after the first click? So it's not actually there, but if that's the case how would I access those elements?


    Thank You,
    DT
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 655

    #2
    Post the specific/minimum code able to reproduce the error.

    Comment

    Working...