user4621642
user4621642

Reputation:

Function returns 'undefined' value

I have this code below:

alert('Returned value : ' + myid_templates_editor_image_id_generator());


//Generates unique id for every image created
function myid_templates_editor_image_id_generator(){    
    (function($){
        var a = 1;
        while(true){                    
            if($('#myid_templates_editor_image_' + a).length == 0) {                
                alert('Inside value : ' a);
                return a;               
            }
            a++;                            
        }
    })(jQuery); 
}

It alerts two times:

enter image description here

enter image description here

Why is the return value 'undefined'? It must be 1. Where am I missing?

Upvotes: 5

Views: 98

Answers (1)

Claudio Redi
Claudio Redi

Reputation: 68400

Function myid_templates_editor_image_id_generator is returning nothing. The only return statement you have (return a;) exits function($){...} but it's not propagated to outer function.

Upvotes: 3

Related Questions