1

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
YannickIngenierie
  • 602
  • 1
  • 13
  • 37

3 Answers3

1

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') );
PeterKA
  • 24,158
  • 5
  • 26
  • 48
0

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

dekkard
  • 6,121
  • 1
  • 16
  • 26
  • my probleme is not tfo Or TFO. But k. In final I've $Root.data("TFO")[k] instead of $Root.data("TFO")[Toto"] – YannickIngenierie Apr 26 '15 at 16:12
  • Sorry then I still didn't get your problem. You defined your data to have the key of 'k' and the value of 'Toto' so accessing ```$Root.data("TFO")['k']``` is the only correct way to get 'Toto' and you can't do ```$Root.data("TFO")['Toto']``` since 'Toto' is not a key. – dekkard Apr 26 '15 at 16:20
  • k is a variable from function(n,k,v){...} k is not the real name but inside (Toto) is the name I Want – YannickIngenierie Apr 26 '15 at 20:17
0

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"))); }
Community
  • 1
  • 1
YannickIngenierie
  • 602
  • 1
  • 13
  • 37