tags:

views:

1380

answers:

5

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?

+1  A: 
      $("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

synhershko
where do i put this? When i write that in my head in the document.ready part, it`ll just break the rest of my js.
mourique
Put it, surrounded by <script></script>, at the end of your page, right before </body>, with jQuery that's parallel to using document.ready.
synhershko
+2  A: 

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>
Jonathan Sampson
A: 
$('#divwithimage').hover(
       function(){$('#ptag').show();}, //shows when hovering over
       function(){$('#ptag').hide();} //Hides when hovering finished
);
AutomatedTester
Don't forget your comma to split up the two functions.
Jonathan Sampson
Sorted, Thanks Jonathan!
AutomatedTester
No problem. Keep up the good work :)
Jonathan Sampson
i get this from firebug:missing ; before statement
mourique
it may have been because of my missing comma between the two functions. I have just tried it and it worked for me
AutomatedTester
there is no error anymore, but the effect is still not working. Could it be that the javascript is kinda broken because i created the whole page with includes and a php switch?
mourique
A: 
<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>
Brad Gignac
A: 
<div id="wrapper">
<div id="grid">
<div class="box"><img src="img/an_005_BUR_thebeat.jpg" width="310px" height="438px" />3 index<p class="hoverbox">some information</p></div>
<div class="box"><img src="img/an_014_GUE_homme_a.jpg" width="310px" height="404px" />4 Culture</div>
<div class="box"><img src="img/an_044_BVL_springmotiv.jpg" width="310px" height="310px" />5 Pharma</div>
<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>
</div>
</div>
<script type="text/javascript">
    $('div.box img').hover(
        function() { $('div.box p.hoverbox').show(); },
        function() { $('div.box p.hoverbox').hide(); }
    );
</script>

This is how it looks according to Brads solution. But its not working. Could it be that the js is not correctly loaded because it is inserted via php switch?

mourique
do i have to hide the <p> via css before all that?likep.hoverbox {display:none;}or something?
mourique