0

I'm new to JS and would like to create 10 vertical lines in my webpage. I have written my HTML code as

  <div id="verticle-line"></div>

and in my CSS I have

#verticle-line {
width: 1px;
min-height: 400px;
background: red;  
margin:15px;  
float:left
}

How Can I create 10 such lines in my webpage using JavaScript?

3

2 Answers 2

4

There are many ways to do this but the easiest would probably be this:

for(var i=0; i<10; i++) {
  document.write('<div class="verticle-line"></div>');
}

Use a for loop to write 10 divs on your page. I also changed id to class, because you should not have more than one element with the same id on your page. Make sure you change your CSS to match a class.

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

Comments

3

See this -

for(x=0; x<9;x++) {
    var vertical = document.createElement('div');
    vertical.className = "verticle-line";
    document.getElementById('wrapper').appendChild(vertical);
}
.verticle-line {
  width: 1px;
  min-height: 400px;
  background: red;
  margin: 15px;
  float: left
}
<div id="wrapper">
  <div class="verticle-line"></div>
</div>

3 Comments

You shouldn't have multiple elements with the same class. It would better be changed to class
In that case, we can use vertical.className = "verticle-line"; instead of vertical.id = "verticle-line"; Will update the snippet anyway
@Jinu Kurian is there a way to create some horizontal lines over vertical ones so that I get a graph-like drawing.

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.