2

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.

3
  • Note that $(Object.moveMe) is basically doing $($(this)), which is superfluous. Commented Mar 2, 2012 at 18:12
  • I am not sure, but I think you need to use var Objects = new Object; Commented Mar 2, 2012 at 18:15
  • @pimvdb It'll be getting used in other functions, that's why it's needed. The example above is just to outline the problem I was having. Commented Mar 2, 2012 at 18:19

2 Answers 2

3

try: http://jsbin.com/ocodoz/

var a;
alert(a);

a === undefined But declared in the current scope..

Your Object have to be set to an object

var Objects = {};
Sign up to request clarification or add additional context in comments.

Comments

2

It's not a scope issue. The problem is that, as the error says, Objects is undefined. It looks like you want to set a property of it, so initialize it as an object literal:

var Objects = {};

Currently, what you are trying to do is effectively:

undefined.moveMe = $(this);

When you declare a variable, its value is undefined until you assign some other value to it. By assigning an empty object literal to it, you can then set properties of that object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.