3

I need to build a widget that contains 3000 squares. Doing this manually would take a very long time, maybe some of you know the easiest way to generate the class .square 3000 times? I need also be able to alter the content of each square, for example a color, title, etc. Thx friends!

<div class="square">
  <h1>10</h1>
</div>

https://jsfiddle.net/srowf8hg/

connexo
  • 53,704
  • 14
  • 91
  • 128
Lukas
  • 159
  • 2
  • 4
  • 18

4 Answers4

2

You just need a loop and create a new square on each iteration. In order to be able to access each square individually, each generated square gets its own unique id:

var cont = document.getElementById("container");

for(var i = 1; i <3001; ++i){
 var div = document.createElement("div");
 div.setAttribute("class", "square");
 div.setAttribute("id", "div" + i);
 
 var h1 = document.createElement("h1");
   h1.textContent = i;
   div.appendChild(h1);
   cont.appendChild(div);
 }
.square{
  background:#fa5339;
  color:#fff;
  width:100px;
  height:100px;
  float:left;
  border:1px solid #d63d26;
}
h1{
    height: 50%; 
    width:100%;
    display:flex;
    align-items: center;
    justify-content: center;
}
<div id=container>
  
</div>
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
1

Your question is very vague.

What technologies are you willing ( or able in the case of homework like project) to use to achieve your goal?

If you have no idea how to do it then i would suggest you start learning of some jQuery, that is a javascript framework, that allows you to do some pretty cooll and easy stuff.

If you do end up using jquery, all you would have to do is to create an element lets say:

<div id="container"></div>

and then somewhere in your javascsript you would have a javascript array with the objects that you are rendering for, say an object named square { color,title,text,name,this,that }

And after that you could just create a loop,construct your html elements as string and use jquery to append the elements in your DOM. An example would be :

var myContainer = $('#container'); <--- this holds a reference to the container element using jquery
for(var i=0,len=myArray.length;i<length;i++){ <-- in my array you would have your "square" objects so that you can modify each square based on parameters like name,title,color etc
    var elementInString = '<div class="square" style="color:"'+myArra[i].color+'>';   <-- and create your parameterised square here
    $(myContainer).append(elementInString);
}

This is one way to do it. Other way include using other frameworks (Knockout,Angular etc)

I hope this helps

MKougiouris
  • 2,821
  • 1
  • 16
  • 19
  • This is a good approach. You should make the code a little more readable though (long lines). – connexo Nov 20 '16 at 18:34
  • Why bring JQuery into this? This is a simple problem with a simple pure JS answer. Angular and Kockout are completely unecessary, as are Arrays. – Scott Marcus Nov 20 '16 at 18:36
  • To be honest i dont really know how to write proper code blocks here yet! – MKougiouris Nov 20 '16 at 18:39
  • Thank you MKougiouris for the explanation. This is kind of homework, I will stick at jQuery at the time but I will look at all technologies, all these solutions. – Lukas Nov 20 '16 at 18:40
  • 1
    I bring in jQuery because from his picture i can see he must be student, so its a good time for him to experiment with jQuery and learn new things,in case he does not know of jquery.Also that why i am bringing in knockout and angular, he is doing the first steps in creating responsive behaviours on dom elements,what a better time to try to make him to google a bit around angular and knockout :) – MKougiouris Nov 20 '16 at 18:43
  • @MKougiouris I would strongly advise against that approach. I am an IT trainer (have been for 20 years) and I can tell you that no one should learn JQuery before they have first learned JavaScript. In the beginning it may seem helpful, but in the long run it hurts because there are many circumstances where using JQuery instead of pure JS will adversely affect your application.This problem requires a simply JavaScript loop and knowing how to create a DOM element. That's all. No need for libraries and frameworks here. – Scott Marcus Nov 20 '16 at 18:45
  • @MKougiouris Also, Angular and Knockout are frameworks for building SPAs working in a MC-* pattern. They are not meant to be used to solve trivial coding issues. They are meant for building robust applications that adhere to a specific architecture. – Scott Marcus Nov 20 '16 at 18:47
  • You are absolutely right about understanding javascript first,my response here is not to be taken as the response of a mentor, but more as a response from someone who is still learning like me. As for angular and knockout i did not refer to them as answers to this question per se, but more like new technologies that he should look into a bit to see what they are about, i would not suggest for him to dive in them before understanding pure Js first – MKougiouris Nov 20 '16 at 18:54
  • @MKougiouris Again I ask why in the world you would recommend someone start looking into JavaScript frameworks and libraries without first knowing JavaScript? That makes no sense and will only confuse. It's like saying go learn calculus before learning algebra. – Scott Marcus Nov 21 '16 at 14:16
0

var container = $('#container');
for (var i = 0; i < 3000; i++) {
  container.append($('<div class="square"><h1>10</h1></div>'));
}
.square {
  background: #fa5339;
  color: #fff;
  width: 100px;
  height: 100px;
  float: left;
  border: 1px solid #d63d26;
}
h1 {
  height: 50%;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container"></div>
connexo
  • 53,704
  • 14
  • 91
  • 128
  • The OP said he needs to be able to access each square individually. Your solution doesn't give him a way to do that easily. – Scott Marcus Nov 20 '16 at 18:28
  • So that's a reason to downvote my answer? It does answer the core question. – connexo Nov 20 '16 at 18:30
  • Your solution does not address one of the core requirements of the question, so it doesn't really provide a good answer. – Scott Marcus Nov 20 '16 at 18:31
  • There is only one core requirement, that is in the title of the question. The others are "I also need to"-requirements. – connexo Nov 20 '16 at 18:32
  • You gave an answer that doesn't meet the OPs criteria. That's not a good answer. If I walked into a code review and told my peers that my code solves 50% of the problems it was supposed to solve, do you think it would be accepted. Don't take it so personally. – Scott Marcus Nov 20 '16 at 18:41
0

the best way would be to use JavaScript to make them for you

an example that I made here will do what you want, and also set each one a unique id so they can be edited.

<div id="square_container"> </div>
<script>
    var i, square, text, container = document.getElementById("square_container");
    for (i = 1; i <= 3000; i += 1) {
        square = document.createElement("div");
        square.id = "square" + i;
        square.classList.add("square");
        text = document.createElement("h1");
        text.innerHTML = i;
        text.id = "text" + i;
        square.appendChild(text);
        container.appendChild(square);
    }
</script>
Philip Eagles
  • 888
  • 1
  • 9
  • 27
  • `innerHTML` should not be used when the value isn't going to contain any HTML. – Scott Marcus Nov 20 '16 at 18:32
  • @ScottMarcus I don't unerstand what you mean by it not containing HTML? Do you mean that it should only be used to set more HTML tags inside itself and not just used to set text? – Philip Eagles Nov 20 '16 at 18:35
  • @PhilipEagles Scott is just trying to find flaws in other people's answers, so he can criticize them. Helps make other's answers look faulty so his chances to get his answer checkmarked rise. Worked this time! – connexo Nov 20 '16 at 18:37
  • @connexo I'm just trying to explain something that is commonly misunderstood. `innerHTML` takes in a string and that string is then parsed for HTML content. In cases where the string does not/will not contain any HTML, you shouldn't use it as it causes more internal work than is necessary. The `.textContent` property would be the correct property to use. – Scott Marcus Nov 20 '16 at 18:39
  • You really speeded up my work. Used technology doesn't matter for me at the point, the project is more complex. All your answers are really helpful for me, thank you so much guys! – Lukas Nov 20 '16 at 18:58