views:

50

answers:

4

hey, Im trying to make a call a jquery function and pass some args with it in the form of

$('#button').mouseenter(exampleFunction(arg1,arg2));

function exampleFunction(arg1,arg2)

The function works fine with no args written like this.

$('#button').mouseenter(exampleFunction);

function exampleFunction;

but as soon as i add () to put args in the function stops working.

like this:

$('#button').mouseenter(exampleFunction());

It seems like this is some sort of jquery syntax error on my part

here's the actual code

    <script type="text/javascript">

$(document).ready(function() {
$('.section').mouseover(function(){
  $('#nav2').fadeOut(0).animate({"height":"30px"}, 250);

         });


$('#section1').hover(navSelect);

function navSelect(){
  if ( $('.interior').is(':hidden')){
  $('.subSection').fadeOut(250);
  $('.interior').delay(250).fadeIn(250);
  $('#selector').animate({"left":"0px"},250);
  }}


$('#section2').mouseenter(function(){
  if ( $('.exterior').is(':hidden')){

  $('.subSection').fadeOut(250);
  $('.exterior').delay(250).fadeIn(250);
  $('#selector').animate({"left":"100px"},250);
  }
});
$('#section3').mouseenter(function(){
  if ( $('.view').is(':hidden')){

  $('.subSection').fadeOut(250);
  $('.view').delay(250).fadeIn(250);
  $('#selector').animate({"left":"200px"},250);
  }
});


         });
</script>
+1  A: 

the difference between funktion and funktion() is one is a pointer to a function and the other is the result of function execution.

try

 mouseenter(function(a,b){....})

there you are defining the function and passing it in. The function defined takes 2 args, a and b.

hvgotcodes
What im trying to do is make one jquery function, that does different animations depending on what args are passed to it. Basically the args would tell the animation which elements to fade in or out, or where to position an element. So basically im hoping to make a global function that can do many things depending on what args it is given.
Black Magic
@James, you don't want one master function. Why not just have a bunch of functions, and call each at the appropriate time?
hvgotcodes
I was looking at adding this plugin to my page, and i noticed the way he was triggering the animations, and thought i could expand on this by adding args and making it a master function. Ill take your advice and stick with seperate functions, but is it possible to do it with a master function?http://cherne.net/brian/resources/jquery.hoverIntent.html
Black Magic
@james of course you can do it with one function. but if you do separate functions each function is simpler, which is a good thing.
hvgotcodes
+3  A: 

Functions in JavaScript are first-class objects. You can put them in variables, return them from other functions, etc. When you write $('#button').mouseenter(exampleFunction(arg1,arg2));, you are saying "Run exampleFunction with these arguments, and use its return value as a callback".

To get jQuery to call the function with those arguments later, you can use an anonymous inline function:

$('#button').mouseenter(function() {
    exampleFunction(arg1,arg2)
});

Your no-argument function will get called, and pass the right arguments to the function you actually want to call. It's sort of a poor-man's closure.

interfect
I just thought about that for a copuple minutes and it kind of just blew my mind, cool
Black Magic
JavaScript will do that. It's very Zen.
interfect
A: 

the function you bind to on an event in jQuery is passed the 'event' object from jQuery's invocation of the method.

jQuery("#button").mouseenter(function(evt){[ do something ]});

if you want to do something like

$('#button').mouseenter(exampleFunction(arg1,arg2));

in order to templatize a function that you want to have bound to the event, you could construct a function like so:

function exampleFunction(arg1, arg2){
        return function(evt){
            jQuery(this).animate({width: arg1, height: arg2},250)
        };
}};

having it return the function that is bound (and thus, passed the event object), and then bind it to elements like

jQuery("#button").mouseenter(exampleFunction("50%","50%"))
        .mouseleave(exampleFunction("100%","100%");

Nirvana Tikku
Thanks! that works perfectly, I don't really understand why it works but it does, would someone care to explain?
Black Magic
A: 

I believe there's a jQuery plugin for that.
Look here: http://plugins.jquery.com/

Chase