Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
DIVI, trying to get reference to mobile menu using JS - undefined

The Problem

I'm using the DIVI theme and am simply attempting to get a reference to the mobile menu's ul element (this has a class of 'et_mobile_menu'). Up until now I've not had any issues getting references in javascript, but this particular one always seems to be undefined/null.

What I've attempted

I tried it in a setTimeout and it worked (it found the element with no problems). So, I tried it using:

document.addEventListener('DOMContentLoaded', function() {
    let el = document.querySelector('.et_mobile_menu');
});

This didn't work. So I gave onload a try:

window.onload = function() {
    let el = document.querySelector('.et_mobile_menu');
}

Again, this didn't work so I tried using JQuery like this...

$(document).ready(function () {
    let el = document.querySelector('.et_mobile_menu');
});

Now this did work... every time the reference was valid.

More Information

I'm using a simple DIVI child theme which I created, and my javascript file is being enqueued in the footer. I've specified 'jquery' as a dependency and have tried add_actions() with different priorities. (I've also tried this with getElementsByClassName())

Summary

I'm curious, is there something specific with Wordpress that I'm missing or is there more I can do to pinpoint the reason the JQuery 'ready' function works here but the vanilla JS methods don't?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!