0

(If someone has a better title, go ahead)

I want to insert the value of a variable within a function's parameters.

Here's an example to show the issue. here's a sample of strElements value: elements : "elm1,elm2"

var p_mode = "exact";
var strElements = "elements : ";
if (typeof elements != "undefined") {
    strElements += elements + ',';
} else {
    strElements = "";
}

tinyMCE.init({
    mode: p_mode,
    /* Magic here */
    strElements
    /* other params */
});

I tried doing an eval and it doesn't seem to work either.

eval('tinyMCE.init({
    mode: '+p_mode+','
    +strElements+'
    /* other params */
});');
Jeff Noel
  • 7,500
  • 4
  • 40
  • 66
  • Always avoid using an eval statement, it is one of the most dangerous things you can do. Why not just pass then in as parameters? – Fermis Nov 24 '14 at 18:46
  • as you can see in the first code block, this is what I tried first and it simply does not work. `Unexpected identifier ` The JavaScript interprets the variable as a parameter by itself with no value. – Jeff Noel Nov 24 '14 at 18:52

1 Answers1

2

Avoid eval at (almost) all costs. It's not at all necessary here, anyway.

var p_mode = "exact";

var initParms = {
  mode: p_mode
};

if (typeof elements != "undefined")
  initParams.elements = elements;

tinyMCE.init( initParams );
Community
  • 1
  • 1
Paul Roub
  • 36,322
  • 27
  • 84
  • 93