0

Possible Duplicate:
Is there a way to access a javascript variable using a string that contains the name of the variable?

I have some content scripted to show based on a user's interaction with my prototype. When they click on a certain callout I'm trying to use the rel tag associated with it to feed in the correct variable, but so far I've had no luck.

Here's a small example.

<div class='cool-btn' rel='awesome'>
Content here
</div>

and then

var awesome = 'some content';

$('.cool-btn').click(function() {
    //do stuff
    $('.other-selector').append($(this).attr('rel'));
}

I remember that when you were jumping in and out of variables and selectors in ActionScript (flashback!) you would throw things in brackets [], but I've been unsuccessful in attempting similar things here or finding information regarding that.

Thanks for any assistance-

Community
  • 1
  • 1
John Blythe
  • 603
  • 1
  • 7
  • 21
  • I don't think "rel" is a valid attribute for `
    ` elements, but I could be wrong.
    – Pointy Jan 19 '13 at 01:31
  • What goes wrong when the code runs? – Pointy Jan 19 '13 at 01:31
  • looks similar, but if it's a duplicate i'm missing the point that would help me fix this :\ @pointy, it just appends the string equivalent. in the example above it'd render a string 'awesome,' not the variable awesome – John Blythe Jan 19 '13 at 02:04
  • It is a dupe. Just because it is an attribute does not make a difference. Use your skills. `window[theAttrValue]` or `var myKeys = { "awesome" :"foo"}; myKeys[theAttrValue];` You should probably use a html5 data attribute and not rel. – epascarello Jan 19 '13 at 02:11

0 Answers0