Passing arguments when calling jquery function

Hey, I'm trying to make a jquery function call and pass some arguments with it in the form

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

function exampleFunction(arg1,arg2)

The function works fine without creating such arguments.

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

function exampleFunction;

but as soon as I add () so that the arguments in the function stop working.

like this:

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

This seems to be some jquery syntax error on my part

here is 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>
+3
source share
4 answers

the function that you associate with the event in jQuery is passed to the 'event' object from the jQuery method call.

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

if you want to do something like

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

to templatize the function you want to associate with the event, you can build a function like this:

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

, (, , ),

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

0

JavaScript - . , .. $('#button').mouseenter(exampleFunction(arg1,arg2));, : " exampleFunction ".

jQuery , :

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

no-argument , . closure.

+5

funktion funktion() - , - .

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

. 2 args, a b.

+1
source

I believe there is a jQuery plugin for this.
See here: http://plugins.jquery.com/

0
source

Source: https://habr.com/ru/post/1757422/


All Articles