Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
How to keep everything when inserting custom HTML as innerHTML

Suppose the following example:

let html = `<Parent customAttr={ value }>
    <Child className="container" />
</Parent>`;

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

div.innerHTML = html;
// or
div.insertAdjacentHTML('beforeend', html);

console.log(div);

When inserting the html variable as innerHTML of the div, first letters of <Parent> and <Child> elements become lowercased, camelCase of the attributes are lost and { value } becomes wrapped into double quotes.

Is it possible and how to keep everything without these changes when inserting as innerHTML?

almost 4 years ago · Santiago Trujillo
2 answers
Answer question

0

from mdn web docs :

What exactly happens when you set value of innerHTML? Doing so causes the user agent to follow these steps:

The specified value is parsed as HTML or XML (based on the document type), resulting in a DocumentFragment object representing the new set of DOM nodes for the new elements. If the element whose contents are being replaced is a element, then the element's content attribute is replaced with the new DocumentFragment created in step 1. For all other elements, the element's contents are replaced with the nodes in the new DocumentFragment.

so you can't do what you want with innerHtml

almost 4 years ago · Santiago Trujillo Report

0

You can simply add content to the element.

div.innerHTML += html
almost 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!