Callback function > return string

I would like to get the word ‘test’ from the callback function psCallback below.

alert(opt); is now displaying the word ‘test’

What am I doing wrong?



function psCallback() {
	var content = 'test';
	
	return content;
}
	
	
jQuery(document).ready(function () {
	
	var opt = {callback: psCallback};
		
	alert(opt);	
		
	return false;

});	

Hi,

A couple of pointers:

You don’t need jQuery to do this.
You don’t need to wrap your code in a call to $(document).ready().

That said, the reason why you aren’t seeing the results you expect is that opt is an object literal and you are just alerting its string representation to the screen.
You can inspect what is happening a little better if you write console.log(opt);, as the console will format things a little nicer for you.

What you need to do is to alert the object’s callback attribute, which is a pointer to your function.

e.g.

function psCallback() { 
  var content = 'test'; 
  return content; 
} 

var opt = {callback: psCallback}; 
alert(opt.callback()); 

Hope that helps.