JavaScript If I click, I want to move the box [duplicate] mouse click wont move

This question already has answers here:
Answer:1

document.getElementsByClassName() returns a HTMLCollection, not a single element, so you have to attach the event listener to every element in the collection.

const move = document.getElementsByClassName("box");

[...move].forEach(m => m.addEventListener("click", _move));

function _move() {
  this.classList.add("playing");
}
.box {
  width: 200px;
  height: 200px;
  background-color: red;
}

.playing {
  transform: translate(200px);
}
<div class="box"></div>
Answer:2

Your css property for moving it to the left isn't right. It should be

.playing {
transform: translateX(200px);
}
Answer:3

You can also use that. instead of using document.getElementsByClassName, use document.querySelector

const move = document.querySelector(".box");
move.addEventListener("click", _move);
function _move(e) {
  move.classList.add("playing");
}
  .box {
    width: 200px;
    height: 200px;
    background-color: red;
  }
  .playing {
    transform: translate(200px);
  }
<div class="box"></div>
Answer:4

As this question is already solved but i was just experimenting if any one wants to move the box continuously by clicking on the box then this code will be helpful.

<head>
  <style>
    .box {
      position: relative;
      width: 200px;
      height: 200px;
      background-color: red;
    }
  </style>
  <title>Document</title>
</head>
<body>
  <div class="box"></div>
  <script>
  let offset = 0;  
  const move = document.getElementsByClassName("box")[0];

  move.addEventListener("click", _move);

    function _move(e) {
      offset += 200;
      move.style.left = offset + 'px';

    }
  </script>
</body>  
 
Answer:5

I have a function that is triggered when .btn is clicked, it will check if the input and text area with the class .req are filled or not. If not, its suppose to select only the empty elements and add ...

I have a function that is triggered when .btn is clicked, it will check if the input and text area with the class .req are filled or not. If not, its suppose to select only the empty elements and add ...

The code below is a simple Node.js web server that responds to a request when the URL is matched. Researching online about node.js it is stated that once you start your script (node index.js) ...

The code below is a simple Node.js web server that responds to a request when the URL is matched. Researching online about node.js it is stated that once you start your script (node index.js) ...

  1. how does node server work
  2. what server does node js use
  3. does react need node server
  4. what web server does node js use
  5. does node.js need web server
  6. how does node js server work
  7. node server does not respond
  8. node server does not start
  9. does node.js replace web server
  10. what does node server do
  11. does nodejs need a server

I'm trying to take an array of objects and, without mutating the original array, rename a specific key in each object. I've created a new variable and assigned it to the result of using 'map' on my ...

I'm trying to take an array of objects and, without mutating the original array, rename a specific key in each object. I've created a new variable and assigned it to the result of using 'map' on my ...

I tried finding the solution to my problem, but couldnt find it, and was looking for some "best practice examples". I have a nodejs express application and my functions are split in files. For example ...

I tried finding the solution to my problem, but couldnt find it, and was looking for some "best practice examples". I have a nodejs express application and my functions are split in files. For example ...

  1. javascript node js await
  2. js node js await
  3. javascript async await nodejs
  4. javascript await node.js
  5. async await javascript node js