0

I am trying to create a div on click using javascript .All the code I found is using an already created div in the body .But I want the div to be created only on click and as many times the users wants.

document.body.onclick = function () {
    var i;
    i = document.createElement('div');
    i.style.height = "10px";
    i.style.width = "10px";
    i.style.backgroundColor = "black";
    document.body.appendChild(i);
}
4
  • This already works. Your issue is probably that the body is too short. Adding this css makes the code work: html,body{height:100%;} Commented Jul 8, 2018 at 15:02
  • Where did you define this function? Need context. Commented Jul 8, 2018 at 15:02
  • Possible duplicate of How can I create and style a div using JavaScript? Commented Jul 8, 2018 at 15:05
  • @TirthaR in that question there already is a div "main " in the html body . I need to create a div on click and where the mouse is. Commented Jul 8, 2018 at 15:22

3 Answers 3

2

As there is no content in the body and by default there is no area set for body, you are not able to click on the body. Hence your function is not being called at all. Either you set the some area for body or use window.onclick:

window.onclick = function (e) {
  var i, left = e.clientX, top = e.clientY;
  i = document.createElement('div');
  i.style.left = left+ "px";
  i.style.top = top+ "px";
  i.style.position = "absolute";
  i.style.height = "10px";
  i.style.width = "10px";
  i.style.backgroundColor = "black";
  document.body.appendChild(i);
}

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

3 Comments

Thank you . But i want it to create the divs where I click .
@Andrada Why doesn't it say that in the question?
@Andrada, you have to set some additional styles to achieve that. I have updated my answer....please check.
0

Your document.body.onclick function will not work.This is due to there is no content in the body as well as no area set for body in default.

By doing some css changes it can work however you can achieve this using window.onclick event as simplest way.

To create the div on mouse click location you need to set the div's position to absolute, and set its left and top to the mouse position by getting event.clientX and event.clientY

window.onclick = function (e) {
  var i;
  i = document.createElement('div');
  i.style.height = "10px";
  i.style.width = "10px";
  i.style.backgroundColor = "black";
  i.style.position = "absolute";
  i.style.left = e.clientX+'px';
  i.style.top = e.clientY+'px';
  document.body.appendChild(i);

}

5 Comments

But ... I want it to create the divs where I click .
@Andrada You need to set the div's position to absolute, and set its left and top to the mouse position.
@Andrada- I updated my answer based on your requirement.
@NullPointer thank you . It would have taken me ages to figure that one out.
Now the important part of the description resembles to my answer.........
0

You could modify your css. The body is at a height of 0px when it doesn't have any initial content, so this means with what you have you need to click on that zero pixel which is probably impossible. By setting the height of the body (in the css) we can click on it.

document.body.onclick = function(e) {
  //console.log(e)
  let i = document.createElement('div');
  i.style.height = "10px";
  i.style.width = "10px";
  i.style.backgroundColor = "black";
  i.style.position = 'absolute';
  i.style.top = e.clientY + 'px';
  i.style.left = e.clientX + 'px';
  document.body.appendChild(i);
}
html, body { height: 100%; }

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.