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! While you're at it, check out some resources Treehouse students have shared here.

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

JavaScript

Fernando Jimenez
PLUS
Fernando Jimenez
Courses Plus Student 8,455 Points

There is a logical problem with my JQuery, can't figure out why.

so basically the function of this jquery is to add an html element (Phone number) whenever the sticky menu appears as. Basically whenever the user scrolls down, the phone number appears, but if they go all the way up the element added disappears. I found this to be a great way to practice Jquery

here is the page https://advocatesidaho.staging.wpengine.com/ //file path /customjs/fjapp.js

the if statement within the if statement is supposed to not keep adding that element over and over.

//This is my code

jQuery(window).scroll(function (){

var iScrollPos = 0;
var iCurScrollPos = $(this).scrollTop();
let addNumber = jQuery('<a id="stickyPhone" class="formPhone" href="tel:+12089952444">(208) 995-2444</a>');

if(iCurScrollPos > iScrollPos)  //  if the  user scrolls down
{
    if(jQuery('.hmenu_social_holder.hmenu_hide_for_mobile #stickyPhone').length <= 0) // if the element exist, then dont do it.
    {
        jQuery(".hmenu_social_holder.hmenu_hide_for_mobile").append(addNumber);
    }


}

else{
    jQuery('.hmenu_social_holder.hmenu_hide_for_mobile #stickyPhone').remove();   // remove the element once the user has scrolled all the way up.
}

});

Steven Parker
Steven Parker
231,007 Points

It seems to be working on the live site. It appears as you scroll down, and vanishes when you scroll back up.

Can you explain more about what problem you are having?

Fernando Jimenez
Fernando Jimenez
Courses Plus Student 8,455 Points

Nevermind, that actually work so its fine. Thanks for trying to help though.