I have a really simple snippet of code and a really (probably) simple change I need to make.
I can’t access a variable that I need to in my jQuery script:
var Objects; // Used to store stuff
enable_reordering();
function enable_reordering()
{
$('a.move-object').click(function(){
Objects.moveMe = $(this);
$('#image-title').text( $(Objects.moveMe).attr('data-child-title') );
return false;
});
}
When I try to change the value of Objects.moveMe to anything, my browser moans that Objects is not set. (Error: Objects is undefined).
How can I make it so that I can use variables in and out of functions throughout my entire script?
Update:
The error is caused by the line
$('#image-title').text( $(Objects.moveMe).attr('data-child-title') );
where I first try and use the variable.
It’s not a scope issue. The problem is that, as the error says,
Objectsisundefined. It looks like you want to set a property of it, so initialize it as an object literal:Currently, what you are trying to do is effectively:
When you declare a variable, its value is
undefineduntil you assign some other value to it. By assigning an empty object literal to it, you can then set properties of that object.