1

I have an object whose name is mural. I have assigned the name mural to the title of an anchor element. When user clicks and element I want to store the title of the anchor in a variable called sprite. I then want to access that object whose name corresponds to the sprite variable string.

Here is my code:

var mural= new Object();
mural.top='0px';
mural.left=-'510px';

var stamps= new Object();
stamps.top='0px';
stamps.left=-'1886px';

var sprite=$(this).attr('title');

$(".image-holder").css("background-position",'sprite.top, sprite.left');

It's not working because the the variable sprite is just a reference to $(this).attr('title'), how do I make it reference the object?

Oh and I know that .css jquery statement probably wont work, I'm not sure yet of the correct way to have two values that don't need quotations in as the second argument. But I am mainly concerned with the way to get access to the object.

4
  • the title element is not for storing data :( Commented May 8, 2011 at 11:24
  • I know, but it does actually describe the picture contained within the anchor. How else would you suggest I do it? I'd like to know a better, more efficient way. Commented May 8, 2011 at 11:38
  • just define the css for it in the stylesheet? Commented May 8, 2011 at 11:40
  • the css for it changes depending on what has been clicked, that's why I have to change it via js Commented May 8, 2011 at 15:28

3 Answers 3

2

Use bracket notation, e.g. if stamps and mural are global objects:

var sprite = window[$(this).attr('title')];

JavaScript does not parse strings for variable substitution, you have to use string concatenation as the others already showed:

.css("background-position", sprite.top + ' ' + sprite.left);

But depending on the context, maybe it is possible to just use CSS:

.mural {
     background-position: 0px 510px;
}

.stamps {
     background-position: 0px 1886px;
}

JS

$('a').click(function() {
    $(".image-holder").addClass($(this).attr('title'));
});

If this is not possible, I would actually store both sprites in a map:

// in some higher scope
var sprites = {
    mural: {
        top: '0px',
        left: '510px'
    },
    stamps: {
        top: '0px',
        left: '1886px'
    }
};

// in your event handler
var sprite = sprites[$(this).attr('title')];

But without knowing more about the context it is difficult to give a better suggestion.

Sign up to request clarification or add additional context in comments.

1 Comment

Thats great thanks. I have gave them classes on click which has a style associated with it in the stylesheet. Thanks very much!
0

EDIT: I finally know what you are aiming for. Please refers to Felix Kling's answer (the window[$(this).attr("title")] part.) and I think his meets your need. :-)


Replace 'sprite.top, sprite.left' into sprite.top + "," + sprite.left

Properties of elements cannot store any variable, but only string.

If you want a dynamic access to the mural Object, just replace sprite into mural from above.

Therefore, I assume that you want an access to the data of mural Object instantly at clicking the element.

JSON.stringify(mural) and save to the title property of the A Element.

When it needs to be extracted, simply spite = JSON.parse($(this).attr("title")) and get that time of Object.

More than this, I suggest you to store it in a new variable instead of storing it at title property if I meet your real expectation.

1 Comment

Thanks. Do you know anything about referencing the object? sprite.top is currently returning 'undefined'.
0

It is easier to use object literals (I'll assume =- is a typo):

var mural = {top:'0px', left:'510px'};
var stamps= {top:'0px', left:'1886px'};

var sprite = this.title;

I would assume that the above will set sprite to be a string primitive, therefore it is unlikely to have a top or left property, but presuming you fix that and sprite is an object (or perhaps you meant to use mural or stamps), then:

$(".image-holder").css("background-position", sprite.top +
  ' ' + sprite.left);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.