0

I would like to be able to press a button that says "hide" and then say "show" when the user clicks the button. In the code below the toggle part works, but now I need the "hide" text to turn to "show" once the content is hidden.

I took the toggle code from: http://thedesignspace.net/MT2archives/000298.html#.UW61YqKG33U

 <script type="text/javascript">
    function toggle(element) {
    document.getElementById(element).style.display = (document.getElementById(element).style.display == "none") ? "" : "none";
    }
    </script>

    <div class="ISBody">
     <h5>Header</h5>
     <div class="ISTopLink"><a href="#ISTop">Return to Top</div>
     <div class="ISHide"><a href="javascript:toggle('pos')">Hide Products - </a></div>
     <hr>
     <div id="pos" style="display: block;">
      <div class="ISProductBody">
       <div class="ISSubHead"><A HREF="#">Prodcut Name</A></div>
      <div class="ISList">
       <ul>
        <li>Text here</li>
        <li>Text here</li>
        <li>Text here</li>
        <li>Text here</li>
      </ul>
      </div>
    </div>
    </div>
Rayray Silby
  • 33
  • 1
  • 1
  • 4

6 Answers6

5

Change your button code to something like this:

<div class="ISHide" onclick="toggle(this)"><a href="#" >Hide Products</a></div>

And change your script to something like this

<script type="text/javascript">
function toggle (t) {
if (t.childNodes[0].innerHTML == "Hide Products") {
    t.childNodes[0].innerHTML = "Show Products";
} else {
    t.childNodes[0].innerHTML = "Hide Products";
}
}
</script>
seaBass
  • 585
  • 1
  • 6
  • 17
3

Since your question is tagged jQuery, use jQuery:

<div class="ISHide"><a href="#" class="hideLink">Hide Products - </a></div>


$(".hideLink").on("click", function(){
    if($(this).text()=="Hide Products - ")
    {
        $(this).text("Show Products - ");
    } else {
        $(this).text("Hide Products - ");
    }
    $(".ISProductBody").toggle(); 

    return false;
});

Example: http://jsfiddle.net/calder12/KCj3r/

Rick Calder
  • 18,310
  • 3
  • 24
  • 41
2

My "answer" is a modification of Rick Calder's answer. I used the code provided for a similar scenario, but it had HTML tags inside the button (in this case, Font Awesome icons). I figured it might help some people apply the solution to other situations.

The only modifications I had to do was to change ".text" for ".html" and added ".addClass" / ".removeClass" to change the button styling. (CSS is not included since it is trivial here.)

Here is the code I used:

$(".expand-button .button").on("click", function(){
    if($(this).html()==" Watch Video <i class=\"fa fa-play\"></i> ")
    {
        $(this).html(" Hide Video <i class=\"fa fa-times\"></i> ");
        $(this).addClass("secondary");
    } else {
        $(this).html(" Watch Video <i class=\"fa fa-play\"></i> ");
        $(this).removeClass("secondary");
    }
    $(".expand-button-content").fadeToggle();
});

Thank you, Rick and the OP.

Ricardo Andres
  • 335
  • 3
  • 13
0
var button = document.querySelector(".button")
var text   = document.querySelector(".text")
var t
button.addEventListener("click",function(){


    if(text.textContent=="Like"){
        t = "Liked"
    }
    else{
        t="Like"
    }
    text.textContent=t;
})

This is an Example of a Like button which changes to Liked. The approach is really simple, if the text is Like, the variable t is set to "liked" and vice versa AND t then replaces the text content. Worked for me Cheers, :D

0

TextBox.innerHTML = " ";
ButtonId.onclick = function(){
if (TextBox.innerHTML == " ") {
TextBox.innerHTML = "HI";
}
else {
TextBox.innerHTML = " ";
}
}
<a href="http://fbgadgets.blogspot.com/2014/01/link-change-into-button.html">ReadMore:</a>
<img id="ButtonId" src="https://www.w3schools.com/tags/smiley.gif" width="107" height="98">
<p id="TextBox"> </p>
0

The following shows the button text that is being toggled clearly at the top of the function. It makes use of the Javasript ternary operator for brevity. It would also allow you to cycle through more than two text labels if that was necessary. If only toggling two values, then the ordering of the label values in labels does not matter .

$(".expand-button .button").on("click", function(e){
  const labels = ['Show', 'Hide']
  let idx = labels.indexOf(e.target.innerHTML) + 1
  idx == labels.length ? idx = 0 : idx
  e.target.innerHTML = labels[idx]
});

And an even shorter solution using just the ternary operator if only toggling two values:

    e.target.innerHTML == 'Show' ? e.target.innerHTML == 'Hide' : e.target.innerHTML == 'Show'
Dan Swain
  • 2,910
  • 1
  • 16
  • 36