0

I want to show the buttons on hover and hide them when moused out, i tried the following code but doesnt work.

<div class="mybooks">
    <div class="divbutton">
         <input  class="btnsubmit"  type="button" value="Edit" id="edit_trivia">
         <input  class="btnsubmit"  type="button" value="Delete" id="delete_trivia">
    </div>
</div> 
".$Tri_IMAGE."
".$Tri_CAPTION."
</div>";
}
?>
 </div>
      <!--close accordion-->
   </div>
   <script>
   $( ".mybooks" ).mouseenter(function() {
      $('.divbutton').show();
  });

  $( ".mybooks" ).mouseleave(function() {
  $('.divbutton').hide();
 });

 </script>
Nik Terentyev
  • 2,270
  • 3
  • 16
  • 23
user3196424
  • 537
  • 3
  • 8
  • 16

3 Answers3

1

Just hide the div first

//hide the div here
var $btn = $('.divbutton').hide()
$(".mybooks").mouseenter(function () {
    $btn.show();
});

$(".mybooks").mouseleave(function () {
    $btn.hide();
});

Demo: Fiddle


Or use a css rule to hide it

.divbutton {
    display:none
}

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • This works nicely, but only if you have the text ".$Tri_IMAGE." ".$Tri_CAPTION.". If you delete the text and run it, nothing will happen, the div will have zero height. – Birrel Jan 20 '14 at 02:27
  • You have to assume that those two variables have a value – greg5green Jan 20 '14 at 02:30
0

You should put your javascript code in this function:

$( document ).ready(function() {
    //your code
});

Your code: http://jsfiddle.net/VGJ5u/

Minh Nguyen
  • 490
  • 1
  • 3
  • 8
  • You don't have to put jQuery in the document onload. That is only if the jQuery is being loaded before the DOM elements you want to act on. Here the element are on the page before the jQuery is called so this is unnecessary. – DutGRIFF Jan 20 '14 at 02:22
  • There's no guarantee the page is fully rendered and the DOM elements available to the script if you don't wait until the document is ready as Minh suggests. – RET Jan 20 '14 at 02:23
  • we're getting out of the topic, the concern is show and hide lement via jquery – user3196424 Jan 20 '14 at 02:23
  • Your question as written does not show you appreciate the point being made. – RET Jan 20 '14 at 02:24
  • @RET The javascript is executed as it is read. The HTML is read before the javascript. I am pretty sure this is the case but if I am wrong please point me to any documentation that would contradict my statement. I can't find the docs right this second but check this out: http://stackoverflow.com/questions/4643990/jquery-is-document-ready-necessary – DutGRIFF Jan 20 '14 at 02:31
  • @DutGRIFF - my comment was intended for the OP. Our posts were almost simultaneous. I don't disagree with you, but it's not clear whether the original question was (over) simplified, or whether document.ready could be a factor. – RET Jan 20 '14 at 03:54
0

This fiddle shows the buttons hiding (display: none;) when you hover over them. The thing is, you can't have them reappear once they're gone. There is nothing to hover over...

<div class="divButtons">
    <input  class="btnsubmit"  type="button" value="Edit" id="edit_trivia" />
    <input  class="btnsubmit"  type="button" value="Delete" id="delete_trivia" />
</div>

JavaScript:

$('#edit_trivia').hover(function(){
    $(this).css('display', 'none');
});

$('#delete_trivia').hover(function(){
    $(this).css('display', 'none');
});
Birrel
  • 4,754
  • 6
  • 38
  • 74