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

185
Views
I want to make a div appear when you click a button

I need help with creating a div that when you click a button, the div with custom HTML inside gets created.

<html>
<link rel="stylesheet" href="style.css">
   <script type="text/javascript">
   var template = `
   <div class="block">
      Hello World!
      </div>
`
function createBlock() {

}
   </script>
   <button onclick="createBlock()">Create div</button>
</html>

If this is possible(it most likely is), I am making a post box thing.

Thanks!!!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

are you looking for something like this?

var template = `
   <div class="block">
      Hello World!
      </div>
`
function createBlock() {
  let myDiv = document.createElement('div');
  document.body.append(myDiv);
    myDiv.outerHTML = template;
}
.block{ background: red }
<html>
<link rel="stylesheet" href="style.css">
   <button onclick="createBlock()">Create div</button>
</html>

If you want put your template as a child inside new div you can change your createBlock like this:

function createBlock() {
  let myDiv = document.createElement('div');
  myDiv.innerHTML= template;
  document.body.append(myDiv);
}
about 4 years ago · Juan Pablo Isaza Report

0

Instead of creating an element, a better way to do this (with the same effect) is hiding and then showing the element, like so:

#block {
  display: none;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<link rel="stylesheet" href="style.css">

<button onclick="createBlock()">Create div</button>
  <div id="block">
    Hello World!
   </div>
      
  <script type="text/javascript">
  function createBlock() {
    block = document.getElementById("block")
    block.style.display = "block"
  }
   </script>

</body>
</html>

about 4 years ago · Juan Pablo Isaza 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!