mourique
mourique

Reputation: 163

hover element A, show/hide Element B

I have a <div> element containing an image. Inside that div, I have a <p> element which holds some information about the image. I want to hover the <div> containing the image and then show or hide the <p> element.

<div class="box">
    <img src="img/an_039_AN_diskette.jpg" width="310px" height="465px" />
    6 Pharma IT
    <p class="hoverbox">some information</p>
</div>

Is there an easy way to do so in jQuery?

Upvotes: 4

Views: 23974

Answers (4)

Sampson
Sampson

Reputation: 268354

The following will match all of your images, and target their first sibling having the tag P.

<script type="text/javascript">
$(document).ready(function(){
    $("div.box img").hover(
      function(){$(this).siblings("p:first").show();},
      function(){$(this).siblings("p:first").hide();}
    );
});
</script>

<div class="box">
  <img src="somefile.jpg" />
  <p>This text will toggle.</p>
</div>

Upvotes: 8

AutomatedTester
AutomatedTester

Reputation: 22418

$('#divwithimage').hover(
       function(){$('#ptag').show();}, //shows when hovering over
       function(){$('#ptag').hide();} //Hides when hovering finished
);

Upvotes: 3

Brad Gignac
Brad Gignac

Reputation: 819

<div class="box">
    <img ... />
    <p class="hoverbox">Some text</p>
</div>

<script type="text/javascript">
    $('div.box img').hover(
        function() { $('div.box p.hoverbox').show(); },
        function() { $('div.box p.hoverbox').hide(); }
    );
</script>

Upvotes: 1

synhershko
synhershko

Reputation: 4492

      $("css_selector_of_img").hover(
      function () {
        $("css_selector_of_p_element").show();
      },
      function () {
        $("css_selector_of_p_element").hide();
      }
      );

See http://docs.jquery.com/Events/hover

Upvotes: 6

Related Questions