0

I'm trying to add HTML code with inline CSS styles and to render into HTML page using JS:

var div = document.createElement('div');

div.innerHTML =
    "   <article id='sp-shop-offer' dir='ltr' style='bottom: 0px;width: 100%;height: 100%;left: 0px;z-index: 10000000;letter-spacing: 0;font-family: 'Open Sans'" sans-serif;font-weight: 400;font-size: 12px;color: white;position: fixed;overflow: hidden;'>  "  + 
 /// ETC etc etc
 "           </div>  "  + 
 "       </section>  "  + 
 "  </article>  ";

document.body.appendChild(div);

when I run this code its added to HTML page but seems wthout all css styles.

How to add my HTML code with inline css styles and to render using javascript?

1
  • You can try using jquery Commented May 8, 2020 at 17:24

2 Answers 2

2

You have some syntax errors inside and style attribute, use this:

var div = document.createElement('div');

div.innerHTML =
    "   <article id='sp-shop-offer' dir='ltr' style='bottom: 0px;width: 100%;height: 100%;left: 0px;z-index: 10000000;letter-spacing: 0;font-family: Open Sans, sans-serif;font-weight: 400;font-size: 12px;color: white;position: fixed;overflow: hidden;'>  "  + 
 /// ETC etc etc
 "           </div>  "  + 
 "       </section>  "  + 
 "  </article>  ";

document.body.appendChild(div);
Sign up to request clarification or add additional context in comments.

Comments

1

Using Template literals maybe better and it causes to reduce your error

var div = document.createElement('div');

div.innerHTML =
    `   <article id='sp-shop-offer' dir='ltr' style='bottom: 0px;width: 100%;height: 100%;left: 0px;z-index: 10000000;letter-spacing: 0;font-family: Open Sans, sans-serif;font-weight: 400;font-size: 12px;color: white;position: fixed;overflow: hidden;'>  

          </div>  
       </section>  
   </article>  `;

document.body.appendChild(div);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.