jquery - variables not working when in an object -


so want set couple of variables(datasets) body tag, when combine them object var names not working.

// code

var1 = 'data-var1' var2 = 'data-var2' var3 = 'data-var3'  $('body').attr({    var1 : 1,    var2 : 2,    var3 : 3 }); 

// result after

<body var1="1" var2="2" var3="3">            

as comments in own questions have pointed out, left part of object property declaration name of whole property , cannot variable.

if want achieve result, you'll need refactor code to:

var var1 = "data-var1"; ...  var attributes = {}; attributes[var1] = 1; // <-- can use variables add properties                       // after object literal declaration! ...  $(body).attr(attributes); 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -