Help me in javascript

How to move an element with mouseEvent?

I think you need to be a lot more specific.

Firstly, welcome to the forums.

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://www.freecodecamp.org/learn.

With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).

It is pretty typical on here for people to share a codepen / repl.it / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.

Please provide some example of what you’ve tried and I’m sure you’ll get more help.

Happy coding :slight_smile:

2 Likes

I want to move an element a div for example thanks to the mouse

What code have you tried so far?

I didn’nt try any code cause i don’t know where to start

You have to at least try to Google and try something. It’s always best to do
read-search-ask first.

We do not know what level of JavaScript you have, so it is hard to tell you what to do unless you can show us a start of something.

I want to make my element move left or right depending on the movement of the mouse after clicking and holding the mouse but i don’t know how to do this.

document.querySelector('.myElement').addEventListener('mouseEvent', function(){ document.querySelector('.myElement').classList.toggle("move-right"); document.querySelector('.myElement').classList.toggle("move-left"); }); </script body{ position: relative; } .myElement{ position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 100px; height: 20px; background: yellow; } .myElement.move-right{ transform: translateX(5px); } .myElement.move-left{ transform: translateX(-5px); }

You can use jQuery for that puropse. You have to create draggable element and a droppable element.
Check this link it will give you a better idea : https://jqueryui.com/droppable/