YannickIngenierie
YannickIngenierie

Reputation: 622

jQuery extend with key in variable

I have this function to add key in a data with k="Toto"

 $Root = $("#" + n); $Root.data("TFO", $.extend({ k: v }, $Root.data("TFO")));

But when I see $Root.data("TFO") I get k instead of the value in k

How can I do it?

Upvotes: 1

Views: 672

Answers (3)

PeterKA
PeterKA

Reputation: 24638

Try this:

 var obj = {};
 obj[ k ] = v; //<--------- VALUE of k will be used here & NOT k
 $Root = $("#" + n); 
 $Root.data("tfo", $.extend(obj, $Root.data("tfo")));
 //or $Root.data()['tfo'] = $.extend( obj, $Root.data('tfo') );

Upvotes: 1

YannickIngenierie
YannickIngenierie

Reputation: 622

Sorry, I change my resarch on google and find this then result

function AddK($r, k, v) { var options = {}; options[k] = v; $r.data("TFO", $.extend(options, $r.data("TFO"))); }

Upvotes: 0

unconditional
unconditional

Reputation: 7666

Looks like no matter what is the case in your data-TFO attribute you have to access it in lower case: $Root.data("tfo")

JSFiddle

Upvotes: 0

Related Questions