Skip to content
Advertisement

How can I change the margin of the element with a variable in JavaScript?

I am quite a beginner and trying to learn JavaScript. I am trying to improve an example for Popup.

I want to adjust the left margin of the popup so I can center the element to the text. I added the CSS for reference but I am trying to do it in JS.

Basically the problem is if I put the code as below it works.

JavaScript

But if I define it with variable and put it like below, it won’t work.

JavaScript

I don’t know if I am missing anything. I added the all code below for reference.

JavaScript
JavaScript
JavaScript

Thank you for any help!

Advertisement

Answer

In your example (see below) you tried to set the margin to be equal to a TextNode, which is not a valid value.

JavaScript

You are looking for something like this.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement