Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
33
rated 0 times [  38] [ 5]  / answers: 1 / hits: 51204  / 12 Years ago, tue, august 28, 2012, 12:00:00

Possible Duplicate:

How to convert string as object's field name in javascript






I can do this:



var objPosition = {};
objPosition.title = whatever;


But I'm getting 'title' dynamically, and want to use about a half dozen strings so obtained to assign the half dozen properties to the object. I've tried eval and several other schemes that seem to have the same problem, but have come up empty so far.



I have:



var txtCol = $(this).text();
txtCol = $.trim(txtCol);


and I want the value of txtCol to be a property name.



Any ideas?


More From » javascript

 Answers
9

Use ['propname']:



objPosition[txtCol] = whatever;


Demo: http://jsfiddle.net/hr7XW/


[#83373] Monday, August 27, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tayaw

Total Points: 749
Total Questions: 88
Total Answers: 86

Location: Djibouti
Member since Sun, Feb 27, 2022
2 Years ago
tayaw questions
;