JavaScript How to move a div area to a particular id on a click event?

I have a 10 x 10 board and a button (in the demo given below I've minimized that to 3 x 3 ). When users click on the button the system generates a random number 1 to 9.

document.getElementById("btn").addEventListener("click",function(){

var num = Math.floor(Math.random() * 100);
num = num % 9;/*generate a random no between 0 to 8*/
num = num + 1;/*generate a random no between 1 to 9*/
  alert(num);/* check the no*/
  if(num === "1"){
    /*i need to move the red circle to box no 1*/
    }else if(num === "2"){
      /*i need to move the red circle to box no 2*/
      }
  /*and so on upto 9*/
  });
html,body{
  width:100%;height:100%;
  }

#board{
  width:300px;
  height:300px;
  border:1px solid #000;
  
  }
#one,#two,#three{
  width:100%;
  height:100px;
  float:left;
  }
.flag-point{
  width:100px;
  float:left;
  height:100%;
  }
#a8,#a2,#a4,#a6 {
  background-color:green;
  }

#crcl{
  width:30px;
  height:30px;
  background-color:red;
  border:1px solid #000;
  border-radius:50%;
  }
<div id="crcl"> </div> <button id="btn">move circle</button>

<div id="board">
  <div id="one">
    <div id="a9" class="flag-point">9</div>
    <div id="a8" class="flag-point">8</div>
    <div id="a7" class="flag-point">7</div>
  </div>
  <div id="two">
    <div id="a6" class="flag-point">6</div>
    <div id="a5" class="flag-point">5</div>
    <div id="a4" class="flag-point">4</div>
  </div>
  <div id="three">
    <div id="a3" class="flag-point">3</div>
    <div id="a2" class="flag-point">2</div>
    <div id="a1" class="flag-point">1</div>
  </div>
</div>
Answer:1

I suggest you use document.getElementById and HTMLNode#appendChild

That way you move the circle to be a child of the certain divs.

When you do appendChild on an existing node, it will be detached from wherever it is right now, and moved to the new parent.

document.getElementById("btn").addEventListener("click",function(){

var num = Math.floor(Math.random() * 100);
num = num % 9;/*generate a random no between 0 to 8*/
num = num + 1;/*generate a random no between 1 to 9*/
  alert(num);/* check the no*/
  var parent = document.getElementById('a'+num);
  var circle = document.getElementById('crcl');
  parent.appendChild(circle);
  /*and so on upto 9*/
  });
html,body{
  width:100%;height:100%;
  }

#board{
  width:300px;
  height:300px;
  border:1px solid #000;
  
  }
#one,#two,#three{
  width:100%;
  height:100px;
  float:left;
  }
.flag-point{
  width:100px;
  float:left;
  height:100%;
  }
#a8,#a2,#a4,#a6 {
  background-color:green;
  }

#crcl{
  width:30px;
  height:30px;
  background-color:red;
  border:1px solid #000;
  border-radius:50%;
  }
<div id="crcl"> </div> <button id="btn">move circle</button>

<div id="board">
  <div id="one">
    <div id="a9" class="flag-point">9</div>
    <div id="a8" class="flag-point">8</div>
    <div id="a7" class="flag-point">7</div>
  </div>
  <div id="two">
    <div id="a6" class="flag-point">6</div>
    <div id="a5" class="flag-point">5</div>
    <div id="a4" class="flag-point">4</div>
  </div>
  <div id="three">
    <div id="a3" class="flag-point">3</div>
    <div id="a2" class="flag-point">2</div>
    <div id="a1" class="flag-point">1</div>
  </div>
</div>
Answer:2

you could append the ball to the correct div, not sure if its the best idea, but it does move the ball in there

document.getElementById("btn").addEventListener("click",function(){

var num = Math.floor(Math.random() * 100);
num = num % 9;/*generate a random no between 0 to 8*/
num = num + 1;/*generate a random no between 1 to 9*/
    moveBall(num);
});

function moveBall(num) {
  var ball = document.getElementById('crcl');
  var square = document.getElementById('a' + num);
  square.appendChild(ball);
}
Answer:3

Added a little more css to auto center the circle inside the square.

document.getElementById("btn").addEventListener("click", function() {
  var num = Math.floor(Math.random() * 100);
  num = num % 9; /*generate a random no between 0 to 8*/
  num = num + 1; /*generate a random no between 1 to 9*/
  var currentSquare = document.getElementById("a"+num);
  var circle = document.getElementById("crcl");
  circle.style.display = "block";
  currentSquare.appendChild(circle);
});
html,
body {
  width: 100%;
  height: 100%;
}
#board {
  width: 300px;
  height: 300px;
  border: 1px solid #000;
}
#one,
#two,
#three {
  width: 100%;
  height: 100px;
  float: left;
}
.flag-point {
  width: 100px;
  float: left;
  height: 100%;
  position: relative;
}
#a8,
#a2,
#a4,
#a6 {
  background-color: green;
}
#crcl {
  display: none;
  width: 30px;
  height: 30px;
  background-color: red;
  border: 1px solid #000;
  border-radius: 50%;
  position: absolute;
  margin: auto;
  bottom: 0;
  top: 0;
  left: 0;
  right: 0;
}
<div id="crcl"></div>
<button id="btn">move circle</button>
<div id="board">
  <div id="one">
    <div id="a9" class="flag-point">9</div>
    <div id="a8" class="flag-point">8</div>
    <div id="a7" class="flag-point">7</div>
  </div>
  <div id="two">
    <div id="a6" class="flag-point">6</div>
    <div id="a5" class="flag-point">5</div>
    <div id="a4" class="flag-point">4</div>
  </div>
  <div id="three">
    <div id="a3" class="flag-point">3</div>
    <div id="a2" class="flag-point">2</div>
    <div id="a1" class="flag-point">1</div>
  </div>
</div>
Answer:4

You can use it with jquery like that :

document.getElementById("btn").addEventListener("click",function(){

var num = Math.floor(Math.random() * 100);
num = num % 9;/*generate a random no between 0 to 8*/
num = num + 1;/*generate a random no between 1 to 9*/
  alert(num);/* check the no*/
  var parent = $("#a"+num);
  var circle = $("#crcl");
  parent.append(circle);
  /*and so on upto 9*/
  });
html,body{
  width:100%;height:100%;
  }

#board{
  width:300px;
  height:300px;
  border:1px solid #000;
  
  }
#one,#two,#three{
  width:100%;
  height:100px;
  float:left;
  }
.flag-point{
  width:100px;
  float:left;
  height:100%;
  }
#a8,#a2,#a4,#a6 {
  background-color:green;
  }

#crcl{
  width:30px;
  height:30px;
  background-color:red;
  border:1px solid #000;
  border-radius:50%;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id="crcl"> </div> <button id="btn">move circle</button>

<div id="board">
  <div id="one">
    <div id="a9" class="flag-point">9</div>
    <div id="a8" class="flag-point">8</div>
    <div id="a7" class="flag-point">7</div>
  </div>
  <div id="two">
    <div id="a6" class="flag-point">6</div>
    <div id="a5" class="flag-point">5</div>
    <div id="a4" class="flag-point">4</div>
  </div>
  <div id="three">
    <div id="a3" class="flag-point">3</div>
    <div id="a2" class="flag-point">2</div>
    <div id="a1" class="flag-point">1</div>
  </div>
</div>
Answer:5

I am trying to add br tag between the text in react. can you guys tell me how to add it. I tried adding br tag... but if I add I get tag in the browser. providng my code below. can you guys tell me ...

I am trying to add br tag between the text in react. can you guys tell me how to add it. I tried adding br tag... but if I add I get tag in the browser. providng my code below. can you guys tell me ...

I wrote a simple code to choose random name from four, using "random_word();" function and it doesn't work. It says:"undefined". Can anyone explain me why and help to fix it? Thanks for all help. var ...

I wrote a simple code to choose random name from four, using "random_word();" function and it doesn't work. It says:"undefined". Can anyone explain me why and help to fix it? Thanks for all help. var ...

  1. javascript function returning undefined
  2. javascript function returning function
  3. javascript function returning multiple values
  4. javascript function returning value
  5. javascript function returning boolean
  6. javascript function returning object
  7. javascript function returning promise
  8. javascript function returning array
  9. javascript function returning nan
  10. javascript function returning true or false
  11. javascript function returning another function
  12. javascript function returning two values
  13. javascript function returning 2 values
  14. javascript function returning int
  15. javascript function returning json
  16. javascript function returning a boolean value
  17. javascript function returning json object
  18. javascript function returning html
  19. javascript function returning undefined value
  20. javascript function returning integer

I'm having some trouble with using Dropzone.js within a standard html form. With the code I have, everything is working as it should, I can click the area to add an image, I can only add one image at ...

I'm having some trouble with using Dropzone.js within a standard html form. With the code I have, everything is working as it should, I can click the area to add an image, I can only add one image at ...

  1. dropzone form submit
  2. dropzone submit form with files
  3. dropzone submit form without files
  4. dropzone submit form data
  5. dropzone js form submit
  6. dropzone processqueue on form submit
  7. dropzone upload on form submit
  8. dropzone upload file on form submit
  9. dropzone js upload on form submit

<div contenteditable="true"> abcde<img src="abc.jp" />fg </div> $('div').append('<img src="abc.jpg" />'); //this will append img at end of text I try to add an image into ...

<div contenteditable="true"> abcde<img src="abc.jp" />fg </div> $('div').append('<img src="abc.jpg" />'); //this will append img at end of text I try to add an image into ...

  1. jquery append string to string
  2. jquery append string as html
  3. jquery append string in loop
  4. jquery append string with comma
  5. jquery append string to input value
  6. jquery append string to url
  7. jquery append string to element
  8. jquery append string to href
  9. jquery append string to class name
  10. jquery append string to array
  11. jquery append string to id
  12. jquery append string not html
  13. jquery append string to textarea
  14. jquery append string to body
  15. jquery append string to textbox
  16. jquery append string before
  17. jquery append string to var
  18. jquery append string text
  19. jquery append string object
  20. jquery append string position