1

So, I have following js function:

function check_image(imgs){             
  var img = jQuery(imgs); //This loads the image      
  img.on('load', function(e){ //success
     var display =  'display';                  
  }).on('error', function(e) { //error
     var display =  'display_none';                 
  });
    return display;         
};

Based on whether the image is available or not, I am trying to return a certain value.

I need to return the display var but I am having a hard time why it is not working.

Could someone help me out?

Thanks

Steve Kim
  • 5,293
  • 16
  • 54
  • 99

2 Answers2

2

I dont think you can directly return a value from a on function, as this only activates when the event is activated. As an alternative solution.

You can call another function in the 'on' delegates to then perform the required functionality.

function check_image(imgs){             
  var img = jQuery(imgs); //This loads the image      
  img.on('load', function(e){ //success
     updateDisplay('display', $(this));              
  }).on('error', function(e) { //error
     updateDisplay('display_none', $(this));                
  });    
};

function updateDisplay(displayVar, img) {
    if(displayVar == 'display') {

    } else if(displayVar == 'display_none') {

    }
}
Valeklosse
  • 1,017
  • 3
  • 19
  • 36
0

You return display but not define and set value for it. ( when you write var display, it mean you define a local variable and it not exist outside scope)

https://msdn.microsoft.com/library/bzt2dkta(v=vs.94).aspx

  • Thank you for the reply. I am aware that var display is a local variable. I want to know what the proper way of returning the inner local variable to outside so that it can be returned. Hope this makes sense. – Steve Kim Jun 01 '16 at 07:45