0

i have a button at the bottom of my page, and when the user scrolls to the bottom of the page a class gets added called 'done'. (this is all handled through a plugin).

i'm trying to change the text of this button when this class gets added. i have worked this script up but have no idea why it's not working?

jquery

jQuery(document).ready(function(){
    if ( $('#load-more').hasClass('done') ) {
        $(this).text('No more posts to load');
    };
});

html

<button id="load-more" class="alm-load-more-btn more done">Load more posts...</button>

1 Answer 1

1

$(document).ready() will trigger the function after the page loads. During that time the button will not have the class "done" yet.

You may want to trigger that function after the user has scrolled to the bottom of the page instead. Like so:

$(window).scroll(function() {
   if($(window).scrollTop() + $(window).height() == $(document).height()) {
       $('#load-more').hasClass('done').text('No more posts to load');
   }
});

Another solution would be to use CSS instead.

<button id="load-more" class="alm-load-more-btn more done">
    <span class="load-more">Load more posts...</span>
    <span class="no-more">No more posts to load</span>
</button>

#load-more .no-more,
#load-more.done .load-more {display:none;}
#load-more.done .no-more {display:block;}
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way to do this dynamically? Since it's not always at the bottom of the window
@Jordan I added another solution. Hopefully that helps. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.