Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
19
rated 0 times [  23] [ 4]  / answers: 1 / hits: 17825  / 12 Years ago, mon, july 30, 2012, 12:00:00

I have the following JSON object:



var definitionsObject = {company : Some information about company};


This object will actually contain a lot of definitions, not just one. And I also have the following event handler for a link click which has a custom data-name attribute containing the term company:



$(.definitinOpener).click(function() {
$this = $(this);
var hintID = $this.attr(data-name);
var hintText = definitionsObject.hintID;
});


So, what I'm trying to do is get the value of data-name custom attribute of the clicked link, go to the definitionsObject object and get the value of the field which is equal to the data-name attribute value. However in this way I'm always getting undefined.



Could anybody please help me to figure out what exactly I'm doing wrong?



Thank you beforehand.


More From » jquery

 Answers
9

You can look up a value in an object in two ways.



var obj = { key : 'value' }
var lookup = 'key'

console.log( obj.lookup ) //undefined
console.log( obj.key ) //value
console.log( obj[lookup] ) //value


You probably want this:



var hintText = definitionsObject[hintID];

[#83970] Saturday, July 28, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dorab

Total Points: 22
Total Questions: 106
Total Answers: 99

Location: El Salvador
Member since Fri, May 8, 2020
4 Years ago
;