Reputation:
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:
Why is the return value 'undefined'? It must be 1
. Where am I missing?
Upvotes: 5
Views: 98
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