Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set position of draggable div

I'm doing a little application using jQuery and JQuery ui.

I've defined a dragable div, and it works perfectly, but I want to set the position of the div when a click a button...

I read here in stackOverflow to do this:

element.position().top = topUserDefined;
element.position().left = leftUserDefined;

I did an alert of element.position().top before and after this assignment and the new value it's not assigned, it keeps the original one...

Any Idea??? Thanks!

like image 416
Andres Avatar asked Oct 05 '11 17:10

Andres


People also ask

Can Div be draggable?

Draggable Div is a kind of element that you can drag anywhere.

How do you make a div draggable?

To make an object draggable set draggable=true on that element. Just about anything can be drag-enabled: images, files, links, files, or any markup on your page.

What is the syntax of the draggable function?

The draggable (option) method specifies that an HTML element can be moved in the HTML page. Here, the option parameter specifies the behavior of the elements involved. Syntax: $(selector, context).

How do I make textarea draggable?

All you need to do is define draggable=true in your element and code the relevant ondragstart and ondragend logic. This works with both vanilla JS and frameworks like React.


1 Answers

.position() gets the position. It doesn't set the position. It actually uses element.offsetLeft and element.offsetTop (compared to element.offsetParent position) those are read only.

Change inline CSS value to move your element:

element.css({'top': 10, 'left' : 20})
like image 169
Mohsen Avatar answered Sep 28 '22 19:09

Mohsen