Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

How can i do this?

objPrefix = btn.attr('data-objprefix'); //<button data-objPrefix="foo">
var sendData =  {objPrefix : {"bar":"ccccc"}};

ANd I want the output to be

{"foo" : {"bar":"ccccc"}};

but instead it is

{"objPrefix" : {"bar":"ccccc"}}
share|improve this question

1 Answer 1

up vote 6 down vote accepted

When you use the literal notation objPrefix is considered as the keyname itself and not value of the variable objPrefix, Instead try use bracket notation to set the property name for the object based on a variable value. So try this way:

var sendData = {};
sendData[objPrefix] = {"bar":"ccccc"};

Also you can infact use jquery data-api to fetch the value of data-attribute i.e

objPrefix  = btn.data('objprefix')
share|improve this answer
    
awesome thanks a lot man... tht worked :) –  ideate Nov 13 '13 at 22:52
    
@ideate You are welcome... –  PSL Nov 13 '13 at 23:03
1  
@ideate if it worked, you should mark this as the answer by clicking the check-mark next to the vote count. –  Mike Edwards Nov 13 '13 at 23:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.