-1

I'm pretty new to javascript and I'm trying to recreate this: https://ibb.co/hh45jH

new Box(0, 0, 20, 30, "#007", parentDiv)

doesn't produce anything at all and I'm not quite sure why. I want it to produce a colored div with the above specifications

This is my code so far:

class Box {
  constructor(xPosition, yPosition, width, height, backgroundColor, parentElement) {
    var div = document.createElement("div");
    div.style.width = width;
    div.style.height = height;
    div.style.background = backgroundColor;
    style.position = "absolute";
    style.top = yPosition;
    style.left = xPosition;
  }

  function draw() {
    var parent = document.getElementById("parentDiv");
    new Box(0, 0, 20, 30, "#007", parentDiv)
  }
}
window.addEventListener("load", draw);
.parentDiv {
  width: 500px;
  height: 500px;
  background-color: red;
}
<div class="parentDiv"></div>

I want the above JavaScript code to produce a new div with the given instructions (color, height, width and so on) and then place it in the parentDiv just like in the example (link above).

7
  • 2
    Can you recreate a code snippet describing your problem? Your question is not clear, what bugs/problems do you have? Commented Apr 26, 2018 at 7:39
  • 1
    Please visit the help center, take the tour to see what and How to Ask. Do some research, search for related topics on SO; if you get stuck, post a minimal reproducible example of your attempt, noting input and expected output. Just click the <> snippet editor and add code that shows the output and tell us the expected output Commented Apr 26, 2018 at 7:41
  • new Box(0, 0, 20, 30, "#007", parentDiv) doesn't produce anything at all and I'm not quite sure why. I want it to produce a colored div with the above specifications Commented Apr 26, 2018 at 7:42
  • I created a snippet for you. Please see the console output and fix it Commented Apr 26, 2018 at 7:44
  • You have undefined style and the code runs if you move the function draw out of the class. var div = document.createElement("div"); var style = div.style; Commented Apr 26, 2018 at 7:47

2 Answers 2

0

I made few changes to your snippet. Check this out and fell free to comment if you have any doubt

class Box {
  constructor(xPosition, yPosition, width, height, backgroundColor) {
    this.element = document.createElement("div");
    var style = this.element.style;
    style.width = width + "px";
    style.height = height + "px";
    style.background = backgroundColor;
    style.position = "absolute";
    style.top = yPosition;
    style.left = xPosition;
  }
}

function draw() {
  let parentDiv = document.getElementById("parentDiv");
  let newBox = new Box(0, 0, 20, 30, "#557");
  parentDiv.append(newBox.element);
}


window.addEventListener("load", draw);
#parentDiv {
  width: 500px;
  height: 500px;
  background-color: red;
}
<div id="parentDiv"> asd </div>

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

2 Comments

There's no good reason why Box should be a class. Just make it a function that returns the element.
agree, but changing things too much could lead the OP to a more difficult understanding.
0

As you can see on my snippet, after you define your class properly, you can create an object by this class and use a function from this object to draw it like that.

class Box {
  constructor(xPosition, yPosition, width, height, backgroundColor) {
    this.element = document.createElement("div");
    this.element.style.width = width+"px";
    this.element.style.height = height+"px";
    this.element.style.background = backgroundColor;
    this.element.style.position = "absolute";
    this.element.style.top = yPosition;
    this.element.style.left = xPosition;
  }
  
  draw(){
  var parentDiv = document.getElementById("parentID");
  var newBox = new Box(0, 0, 20, 30, "#007");
  parentDiv.append(newBox.element);
  }
}

const newBoxObj = new Box();


window.addEventListener("load", newBoxObj.draw());
<div id="parentID"></div>

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.