How to toggle a text


Hello all,

(sorry for my english first of all)

In that Javascript/DOM final lesson, text ‘Recent Projects’ is changed to 'Projects Viewed’
when the button is clicked, but anyone has a suggestion to change the text back to 'Recent Projects’
when the tree is hidden, and then to ‘Projects Viewed’ again?
(it does not matter now if that action really makes sense in that context,
I think you know the goal of my question, basically how to change from text A to text B after clicking
and from text B to text A after the second click, etc, etc.)

Thank you!!


Please post your code so we can review it and make recommendations. Thanks.


Hi Roy,

function main(){
  $('.projects-button').on('click', function(){
    $(this).text('Projects Viewed');

In the line in bold the text is changed to ‘Projects Viewed’ but it remains that way forever (unless the whole page is reloaded). I wonder how to make that the text changes to ‘Recent projects’ when the projects tree is hidden (so when hidden -> text ‘Recent Projects’ and when visible text -> ‘Projects Viewed’)… I hope my explanation is clear :slight_smile :slight_smile:

Thank you.


One approach would be test if the active class is set.

    if ($(this).hasClass('active')) {
      $(this).text('Projects Viewed');
    } else {


It works perfectly. That’s exactly the behaviour I was expecting.

I didn’t know that method (hasClass;I’ve been learning more about it at the jQuery API documentation)

Thank you very much!!


This topic was automatically closed after 7 days. New replies are no longer allowed.