0

I have created two functions which will allow me to use them with different CSS classes.

    var CSSElement;
$(document).ready(function(){  


  expandBox (".orange");
  minimizeBox(".orange");

});

function expandBox ($CSSElement){
     //When mouse rolls over  
    $($CSSElement).mouseover(function(){  
        $(this).stop().animate({height:'485px'},{queue:false, duration:600, easing: 'easeOutBounce'})  
    });     
}

function minimizeBox ($CSSElement){
     //When mouse is removed  
    $(CSSElement).mouseout(function(){  
        $(this).stop().animate({height:'50px'},{queue:false, duration:600, easing: 'easeOutBounce'})  
    }); 
}

However, only the function expandBox seems to work. If I hover my mouse away the from the .orange element the box does not contract.

I want these animations to appear as functions as I plan to use them few times within my website. If I put my code like below:

$(document).ready(function(){  

   //When mouse rolls over  
    $($CSSElement).mouseover(function(){  
        $(this).stop().animate({height:'485px'},{queue:false, duration:600, easing: 'easeOutBounce'})  
    });  

  //When mouse is removed  
    $(CSSElement).mouseout(function(){  
        $(this).stop().animate({height:'50px'},{queue:false, duration:600, easing: 'easeOutBounce'})  
    }); 

});

Everything seems to work ok. Is there a reason why the first code does not work but the second one does?

Thanks,

vnayak

1
  • 1
    Could you please provide complete set of information, i.e. HTML and jQuery parts, and also encapsulate it in jsfiddle? Commented May 12, 2013 at 22:50

2 Answers 2

3

I guess you made a typo :

$($CSSElement)

$(CSSElement)

thats why it doesn't work

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Kitty! It works perfectly now. Late night coding is a bad thing for me, I guess!
2

In JavaScript, $ is a perfectly legal character in identifiers (rather than, say, completely forbidden [like in C] or a special sigil [PHP, Perl]). As a result, $CSSElement is a different identifier to CSSElement - if only one is defined, the other won't work. $($CSSElement) and $(CSSElement) are different.

(It is potentially confusing to prefix variable names with $; in JavaScript they work just fine without.)

What is happening here:

  1. Both functions take a parameter named with $.
  2. expandBox uses that parameter with the $.
  3. minimizeBox uses it without the $ (thus using some unrelated variable).

My advice: Change everything to not use prefixed $, like this:

function expandBox (CSSElement){
     //When mouse rolls over  
    $(CSSElement).mouseover(function(){  
        $(this).stop().animate({height:'485px'},{queue:false, duration:600, easing: 'easeOutBounce'})  
    });     
}

function minimizeBox (CSSElement){
     //When mouse is removed  
    $(CSSElement).mouseout(function(){  
        $(this).stop().animate({height:'50px'},{queue:false, duration:600, easing: 'easeOutBounce'})  
    }); 
}

1 Comment

Thanks for the explanation Michaelb958. Much appreciated.

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.