views:

34

answers:

1

Hi, been playing with this for a few hours and can't figure it out.

jsFiddle example here.

Basically I want this to show on hover, and click it to toggle whether it is displayed or not. I thought this was the way to do it with display:block;, but I can't get it to work.

Thanks for any help.

+1  A: 

Currently you're using $(this) in the .click(), but that's the <button> (#related-btn) not the <div> (#show-hide), I think what you want is this:

$("#related-btn").hover(function() {
    $("#show-hide").toggle("slow");
}).click(function() {
    $("#show-hide").toggle();
});

You can see an updated example here

Or if you wanted it animated in both cases, this is a bit shorter:

$("#related-btn").bind('mouseenter mouseleave click', function() {
  $("#show-hide").toggle("slow");
});

Or...if you want not to toggle it, but have a click "pin" it, you can do that like this:

$("#related-btn").hover(function() {
  if(!$(this).data('pinned'))
    $("#show-hide").toggle("slow");
}).click(function() {
  $(this).data('pinned', !$(this).data('pinned'));
});

You can see a demo of that here

Nick Craver
Thanks Nick! :)
Kyle Sevenoaks
@Kyle - Welcome :) Is that the effect you're after? I can adjust the example a bit if needed, just let me know if anything's not quite what you're wanting.
Nick Craver
Thanks again, the basic functionality is there, but when you click it refires the function.. I think. (jq noob) I was after something more like hover: show, click(and mouseleave): stay shown, click again: hide.. if you understand?
Kyle Sevenoaks
@Kyle - Try the last example in the answer, there's a demo link, that's the code for "pinning it", for lack of a better term. - Here's a slightly modified version, hiding immediately when unpinning: http://jsfiddle.net/rfyxD/4/
Nick Craver
Wow, that's exactly what I was after! Thanks very much! I'd +1 again if I could.
Kyle Sevenoaks