Clarke Cribb
Clarke Cribb

Reputation: 269

Javascript show and hide function

I am trying to do the show/hide function with some containers and text. When i click on a certain container I want a paragraph to hide. At the moment I am trying to work out when I click the "left" container, the paragraph "barbertext" to disappear.

<p class="hairtext" style="color:red">We are Thairapy, an independent hair <br> and beauty Salon located in the heart<br> of Exeter. At Thairapy, we care about<br> our clients and our main aim is to go<br> that extra mile and look after every <br> one of our happy customers.</p>

    <p class="beautytext" style="color:red">Our beautician, Shail Dutt has over<br> 10 years of experience within the beauty<br> industry. Shail is a very dedicated and<br> passionate person, she strives to make<br> her clients feel loved and special. </p>

    <p class="barbertext" style="color:red">A decent Mens haircut needn't cost the<br>Earth. We offer top quality Men's haircuts<br> from £7. </p>

    <div class="container" id= "left" >
        <h1 style="color:white"><a>HAIR</a></h1>
    </div>

    <div class= "container" id= "center">
        <h1 style="color:white"><a>BEAUTY<a/></h1>
    </div>

    <div class="container" id= "right">
        <h1 style="color:white"><a>BARBERS</a></h1>
    </div>
</div>

The Javascript that I am working with is this:

<script>
$(document).ready(function(){
  $("#hairtext").click(function(){
    $("barbertext").hide();
  });
  $("#hairtext").click(function(){
    $("barbertext").show();
  });
});
</script>

If anyone could help I would be most appreciated.

Upvotes: 1

Views: 95

Answers (4)

Ngob
Ngob

Reputation: 491

You did wrong with your selector:

$(".barbertext")

I am not sure about what you want, but it seems as you want when the user click on the container, a associated div is showed/ hide. I created a fiddle for that: http://jsfiddle.net/BenoitNgo/0yL02nop/6/

Upvotes: 0

Rajdeep Dosanjh
Rajdeep Dosanjh

Reputation: 1187

I have made the following fiddle that should do what you need http://jsfiddle.net/Lyd9hgh2/

$(document).ready(function(){
    var hidden= false;
  $("#left").click(function(){
      if(hidden){
          $(".barbertext").show();
          hidden = false;
      }else
      {
          $(".barbertext").hide();
          hidden = true;
      }

  });
});

Upvotes: 1

Vijay
Vijay

Reputation: 3023

Corrected the existing code: (as per your need - when I click the "left" container, the paragraph "barbertext" to disappear)

<script>
    $(document).ready(function(){
      $("#left").click(function(){
        $(".barbertext").hide();
      });
      $("#left").click(function(){
        $(".barbertext").show();
      });
    });
    </script>

Improvments:

  1. You can use Ids for containers and paragraphs both for performance
  2. You can use jquery toggle function if you want a show/hide behavior on container click

Upvotes: 0

Gabriele Petrioli
Gabriele Petrioli

Reputation: 195992

You bind both handlers to the same element, so you hide and show each time you click.

Try toggle() so that it alternates between hide/show

$(document).ready(function(){
  $(".hairtext").click(function(){
    $(".barbertext").toggle();
  });
});

(needed to add . to the barbertext selector, and . for the hairtext. # is used for ids)

Upvotes: 3

Related Questions