How do I write a jQuery function so that it returns a value that I can access outside the function? I did something like this but when I tried to access it outside the function, it says “undefined”:
$(document).ready(function(){
function add(y, z) {
$('p').click(function() {
x = y + z;
console.log(x); // shows 3
return x;
});
}
var sum = add(1, 2);
console.log(sum); // shows "undefined"
});
This should work. Not sure why you added the
clickevent there