1

I want to make insert a long, massive HTML code to an html element using Javascript. I know innerHTML is a good way to do that, but inserting long html code using InnerHTML is very hard to edit later.

Here is an example:

function display(){
document.getElementById('result').innerHTML = '<div><form id="form" ><p style="border: 2px solid #a1a1a1;background: #dddddd; width: 300px; border- radius: 25px; height:60px;"><b>Search Engine</b><br /></p> <input style = "margin-bottom:20px"id="search" type="button" value="Search"></input></p>'
}
<div id=result></div>
<button onclick='display()'>Display</button>
I have a very massive html code to insert and it is very hard for me to edit.

Could anyone suggest me an easier way to insert html code or a better solution of doing this?

Thanks for any responds!

1
  • 3
    Put it in a separate file. Even better, use a framework like React to make alterations and interpolations easier. Commented Dec 14, 2021 at 3:13

1 Answer 1

5

You can use a template literal so that you can have more readable formatting.

document.getElementById('result').innerHTML = `
<div>
  <form id="form">
    <p style="border: 2px solid #a1a1a1;background: #dddddd; width: 300px; border- radius: 25px; height:60px;">
      <b>Search Engine</b>
      <br />
    </p>
    <input style="margin-bottom:20px" id="search" type="button" value="Search"></input>
    </p>
`;
Sign up to request clarification or add additional context in comments.

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.