Ogdila Ogdila - 5 months ago 7
Javascript Question

Replacing part of code with variable

How can I replace a ClassName in the code below, with a variable.

I have:

(function (root, factory) {

factory((root.ClassName= {}));

dragged =[].slice.call(_document.getElementsByClassName('ClassName'));

});


How can I replace where i have the ClassNames with a variable like this:

(function (root, factory) {

var x = ClassName

factory((root.ClassName= {}));

dragged =[].slice.call(_document.getElementsByClassName('ClassName'));

});


Note that it is only a part of the code I have, I do not need to change the code, I just need to call a variable where those classnames are appearing.

Answer

Are you perhaps looking for something like this?

function (root, factory) {
  var x = "ClassName";
  factory((root[x]= {}));
  dragged = [].slice.call(_document.getElementsByClassName(x));
};
Comments