change text on show/hide

javascript, jquery

Solution

You can use ternary conditional operator to switch between texts.

 $(document).on("click",".class", function(){
    $(this).next().slideToggle("fast");  
     $(this).text($(this).text() == 'view' ? 'hide all' : 'view');
  });

Problem

I have a some content which I want show/hide using jquery. Till now using below code: ``` $(document).on("click",".class", function(){ $(this).next().slideToggle("fast"); }); <div class="class" Title="Click to view/hide all" id="flip">View &#9660;</div> <div class="class" id="panel"><table> Hello!! </table> </div> ``` I have many such view `div` created and are working inside for loop. My show/hide content is working fine, but I want to change text(on button) as 'show' and 'hide' whenver user clicks. Please help.

Original source