Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Fernando Jimenez
Fernando Jimenez
Courses Plus Student 8,203 Points

How can I make an if statement keep looking for the condition to be met even after the DOM has loaded?

So I got this WordPress site where basically there is a sticky menu function. I want the sticky Menu to have the phone number included ONLY ON THE STICKY MENU.

So I got something like this:

jQuery(document).ready(function() { if(jQuery('#hmenu_load_1').css('position') == 'fixed') { jQuery(".hmenu_social_holder.hmenu_hide_for_mobile").html('<p><a class="formPhone" href="tel:+12089952444">(208) 995-2444</a></p>'); jQuery(".hmenu_social_holder.hmenu_hide_for_mobile").css({"float": "right"}); }; });

how can I make it so once they scroll all the way up, the phone number's display will go "none"?

1 Answer

Ari Misha
Ari Misha
19,323 Points

Hello there! DOM or windows Object ships with the event listeners. The onload event gets invoked when the DOM starts loading. But I'm guessing you don't need that, right? You might need an event listener that gets invoked after the DOM gets parsed and loaded, right? DOMContentLoaded event comes to the rescue.

  document.addEventListener("DOMContentLoaded", function(event) {
    console.log("DOM fully loaded and parsed");

Here is a list of all the DOM events for the future reference.

I hope it helped!

~ Ari

Ari Misha
Ari Misha
19,323 Points

@FernandoJiminez Sure! But didn't you want to use it in If block? With jquery('document).ready(), you wait for the DOM to get ready to attach your own logic but in order to trap it in the If block, you might have to make use of Vanilla JavaScript's DOMContentLoaded event.