0

I'm creating new element with createElement function. It works fine. However, I started to use some external SDK, where I need to setup param-bad parameter for some elements. But it doesn't work.

Code:

  var element = document.createElement("div");
  element.param_good = "value";
  element.param-bad = "value";

While I try to add param-bad with some value, I get an error:

Uncaught SyntaxError: Invalid left-hand side in assignment

Also, param_good of course works fine.

How do I work around this?

n0zz
  • 197
  • 1
  • 1
  • 14

1 Answers1

2

I think you are wanting to set some attributes to the newly created div element. You can do this by using setAttribute().

 let element = document.createElement("div");
 element.setAttribute('param_good', 'value');
 element.setAttribute('param-bad', 'value');
 element.innerHTML = 'Demo text';
 
 document.body.append(element);
Sajeeb Ahamed
  • 6,070
  • 2
  • 21
  • 30