0

I want to show/hide the button with class ".edit-one-button" within ".widget" by using the javascript below. However, the button is not hidden when I first load the page. Any idea what might went wrong?

<script type="text/javascript">
    $(function () {
      $('.edit-one-button').hide();  
      $('.widget').hover(function () {
        $(this).find('.edit-one-button').fadeIn(100);
      }, function () {
        $(this).find('.edit-one-button').fadeOut(100);
      });
    });
</script>
6
  • Good question. Not that either. The button shows up when I first load that page. The hover effect doesn't work either... Commented Jul 20, 2012 at 18:02
  • Have you included the jQuery library? Do you get any Javascript errors in the error console? Commented Jul 20, 2012 at 19:41
  • I included that. I do have an error which is saying another function was not built successfully. Will that affect this part??? Commented Jul 20, 2012 at 19:44
  • check your class names, - what do you get when you type " $('.edit-one-button')" in the console? Commented Jul 21, 2012 at 5:48
  • That's what I got: [input.edit-one-button Edit, input.edit-one-button Edit, input.edit-one-button Edit, input.edit-one-button Edit] Commented Jul 23, 2012 at 15:13

1 Answer 1

2

You could also reverse the code so .edit-one-button has an inline style of display:none then add a class like .active on hover with display:block in the css rather than hide on load using javascript.

.edit-one-button {
  display:none;
}



$(function () { 
  $('.widget').hover(function () {
    $('.widget .edit-one-button').fadeIn(100);
  }, function () {
    $('.widget .edit-one-button').fadeOut(100);
  });
});
Sign up to request clarification or add additional context in comments.

9 Comments

The OP's code already is in document ready. They are using the $(fn) version which works the same as $(document).ready(fn).
Still still good practice. The CSS solution I proposed is also relevant and would work.
The code is already in the ready event. The op is using the shorthand.
Can you show me how to do that using the other way you mentioned? So it will all be done through css instead of javascript?
Removed the downvote as the answer is not misleading now, but it still doesn't fix anything in the original code, it's just another way to do the same thing.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.