0

I am messing around with a small div containing three p's. If the user clicks any of the p's they all disappear. There is a small button to readd the content. I'm looking for a way to remove the content without the div closing up.

Below is that I have.

<script>
    $(function () 
    {
        $("p, button").click(function () 
        {
           $("p").toggle();
        })
    });
</script>
<div id="blarg" class="blarg" style="visibility: visible;">
    <p class="test" style="display: block;">If you click on me, I will disappear.</p>
    <p class="test" style="display: block;">Click me away!</p>
    <p class="test" style="display: block;">Click me too!</p>
</div>
<button>Readd</button>
CBC_NS
  • 1,961
  • 4
  • 27
  • 47
  • 1
    You can find the answer here http://stackoverflow.com/questions/9614622/equivalent-of-jquery-hide-to-set-visibility-hidden – Tommy Ivarsson Jun 03 '14 at 18:54
  • So clicking a paragraph should remove only that paragraph, but what should clicking the button do? – j08691 Jun 03 '14 at 18:56
  • Clicking any of the p's should remove them all. Originally, I was testing to see if toggle would remove the content without removing the content space. I left the button there to put the content back. – CBC_NS Jun 03 '14 at 18:58

3 Answers3

1

The css style visiblity can do this for you. You just need to set it to visibility:hidden like this:

$(function () 
{
     $("p, button").click(function () 
     {
         $(this).css("visibility","hidden");
     })

});
Simcha Khabinsky
  • 1,970
  • 2
  • 19
  • 34
0

Toggle is using "display", wich makes the item occupy space, I think you want to use visibility, wich does not affect layout, just it... visibility.

miguel-svq
  • 2,136
  • 9
  • 11
-1

Set visibility: hidden; on each of the p elements on click.

.hidden {
  visibility: hidden;
}

$("p, button").click(function () {
  $(this).toggleClass("hidden");
});

EDIT: Good catch guys... wrote that too fast.

sir_thursday
  • 5,270
  • 12
  • 64
  • 118