1

attempting to loop through a div in the DOM and dynamically render it 300 times. Having trouble accessing the HTML within the styled element. Code as follows:

Current Render

JavaScript File:

var amount = 5;
for(var i = 0; i < amount; i++){
    var new_div = document.createElement("div");
    new_div.className = "hello";
    document.body.appendChild(new_div).innerHTML;

    console.log("This is repeat " + i)
}

HTML File:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="hello">
      <p>
        Hello World!
      </p>
    </div>
    <script src="app.js"></script>
  </body>
</html>

CSS Style Sheet:

.hello {
  width: 80%;
  margin: 0 auto;
  padding: 20px;
  text-align: center;
  border-style: outset;
  border-width: 5px;
  border-color: #d36135;
}

I want to be able to render the first div multiple times as it is exactly styled.

0

2 Answers 2

2

Why are you accessing innerHTML at the end of the append method? There is no need for that and that causes a problem.

Working example:

var amount = 5;
for(var i = 0; i < amount; i++){
    var new_div = document.createElement("div");
    new_div.className = "hello";
    document.body.appendChild(new_div);

    console.log("This is repeat " + i)
}
.hello {
  width: 80%;
  margin: 0 auto;
  padding: 20px;
  text-align: center;
  border-style: outset;
  border-width: 5px;
  border-color: #d36135;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="hello">
      <p>
        Hello World!
      </p>
    </div>
    <script src="app.js"></script>
  </body>
</html>

Sign up to request clarification or add additional context in comments.

Comments

2

Your code is working fine but it seems you want to clone the first element

var amount = 5;

for (var i = 0; i < amount; i++) {
  let elem = document.getElementById("hello").cloneNode(true);
  elem.id = 'hello_' + i;
  document.body.appendChild(elem);
}
.hello {
  width: 80%;
  margin: 0 auto;
  padding: 20px;
  text-align: center;
  border-style: outset;
  border-width: 5px;
  border-color: #d36135;
}
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>Document</title>
  <link rel="stylesheet" href="style.css" />
</head>

<body>
  <div class="hello" id='hello'>
    <p>
      Hello World!
    </p>
  </div>
  <script src="app.js"></script>
</body>

</html>

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.