1

I'm following a tutorial.

I've included the hover.js with the following code:

$(function() {
    // OPACITY OF BUTTON SET TO 50%
    $(".imgopacity").css("opacity","0.5");

    // ON MOUSE OVER
    $(".imgopacity").hover(function () {
        // SET OPACITY TO 100%
        $(this).stop().animate({
            opacity: 1.0
        }, "slow");
    },

    // ON MOUSE OUT
    function () {
        // SET OPACITY BACK TO 50%
        $(this).stop().animate({
            opacity: 0.5
        }, "slow");
    });
});
3
  • 2
    replace the first $(function() { with jQuery(document).ready(function($){ Oct 5, 2011 at 11:45
  • @OneTrickPony Those two pieces of syntax are equivalent, the former being shorthand for the latter. Oct 5, 2011 at 13:21
  • 1
    @AdamBackstrom - it's not quite the same. The second enables jQuery to be called using the $ namespace. Oct 5, 2011 at 13:26

2 Answers 2

5

Going through your source - jQuery Source - it seeems that noConflict() is getting called right at the end.

To fix your hover.js then would look something like this:

jQuery(function($) {    //Allow the use of $ namespace in this function.
    // OPACITY OF BUTTON SET TO 50%
    $(".imgopacity").css("opacity","0.5");

    // ON MOUSE OVER
    $(".imgopacity").hover(function () {

    // SET OPACITY TO 100%
    $(this).stop().animate({
        opacity: 1.0
        }, "slow");
   },

    // ON MOUSE OUT
   function () {

   // SET OPACITY BACK TO 50%
   $(this).stop().animate({
       opacity: 0.5
       }, "slow");
   });
});

Regards,

2
  • I've read somewhere that I need to change all instances of $ to Jquery, I tried that but it didn't work.. For the code you sent, what does this mean: jQuery(function($) { and why did it work? thanks!
    – Laurize
    Oct 5, 2011 at 23:39
  • 1
    @Laurize, the reason it worked is because jQuery(function($) { basically means the following; A function that will use the jQuery library but allow $ to reference jQuery instead (and not some other library that has been added.) See the $ in the parameter of the anonymous function in your snippet. Ciao! Oct 6, 2011 at 6:08
2

Do not use "$()" in Wordpress use "jQuery()" instead.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.