0
$('.header_menu_item').on('mouseenter',function(){
    var default_img = $(this).find('.header_menu_item_icon_img').attr('src');
    var hover_img = $(this).find('.header_menu_item_icon_img').attr('data-hover');
    $(this).find('.header_menu_item_icon_img').prop('src',hover_img);
}).on('mouseleave',function(){
    $(this).find('.header_menu_item_icon_img').prop('src',default_img);
});

How can i pass default_img var to following mouseleave function? Thanks for any help :)

4 Answers 4

2

One option will be declare a variable outside of the 2 functions.

var default_img = '';
$('.header_menu_item').on('mouseenter',function(){
default_img = $(this).find('.header_menu_item_icon_img').attr('src');
   var hover_img = $(this).find('.header_menu_item_icon_img').attr('data-hover');
   $(this).find('.header_menu_item_icon_img').prop('src',hover_img);
}).on('mouseleave',function(){
   $(this).find('.header_menu_item_icon_img').prop('src',default_img);
});
Sign up to request clarification or add additional context in comments.

Comments

1

Set it as data on the element itself and then retrieve it later

//setting
$(this).data('default_img',default_img);

//retrieving
var img = $(this).data('default_img'); 

Through native js

//setting
this.dataset['default_img'] = default_img;

//retrieving
var img = this.dataset['default_img'];

You could also before hand set up a data-* attribute on the html itself so no need to use JS to set the initial data

<img src="http://example.com/someimg.jpg" data-default_img="http://example.com/someimg.jpg" />

data-* attributes reference

dataset reference

jQuery's data() method

Comments

0

You need to use global variables. A function can only pass variables directly to functions that it calls, not functions that are called outside.

var default_img;
$('.header_menu_item').on('mouseenter',function(){
    default_img = $(this).find('.header_menu_item_icon_img').attr('src');
    var hover_img = $(this).find('.header_menu_item_icon_img').attr('data-hover');
    $(this).find('.header_menu_item_icon_img').prop('src',hover_img);
}).on('mouseleave',function(){
    $(this).find('.header_menu_item_icon_img').prop('src',default_img);
});

4 Comments

default_img can differ if you enter two $('.header_menu_item') but only leave one of them, you have to store default_img by each header_menu_item
@ThomasFellinger Unless they're nested, I doubt that can happen.
i think they just need to be positioned somehow overlapping to get failures
We need to see the HTML to know whether this is important.
-1

you can not pass it directly - you can store it in an outer variable - but you have to do it by each item:

$('.header_menu_item').each(function(){

  var $header_menu_item_icon_img = $(this).find('.header_menu_item_icon_img'),
      default_img = $header_menu_item_icon_img.attr('src'),
      hover_img = $header_menu_item_icon_img.attr('data-hover');

  $(this).on('mouseenter',function(){ 
    $header_menu_item_icon_img.prop('src',hover_img);
  }).on('mouseleave',function(){
    $header_menu_item_icon_img.prop('src',default_img); 
  });
});

2 Comments

Only user wanted to pass default_img variable in mouseleave function.. why each function is needed.. ??
because default_img can differ per menu_item and can and will be overwritten in a global variable

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.