Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a div draggable even from a textarea within?

I'm trying to have a draggable div which can also be dragged from a textarea within it.

html:

<div id="divContainer">
    <textarea id="text"></textarea>
</div>

css:

#divContainer {
    position: absolute;
    left: 10px;
    top: 10px;
    width: 100px;
    height: 100px;
    background-color: blue;
}

#text {
    position: absolute;
    left: 5px;
    top: 5px;
    width: 50px;
    height: 50px;
    background-color: green;
}

jquery:

$("#divContainer").draggable();

I can drag the div if I drag by clicking in the div area, but not if I click into the textarea area.

Is there a way to solve this ?

Here is the jsFiddle

like image 607
MirrorMirror Avatar asked May 30 '13 12:05

MirrorMirror


People also ask

How do I make a div element 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. Our example creates an interface to rearrange columns that have been laid out with CSS Grid.

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

Check Out this fiddle perfect for you

FIDDLE

Code:

HTML:

<div>
 <textarea name="ta" id="ta" cols="30" rows="10"></textarea>
</div>

CSS:

div {
 background-color:#aaa;
 padding: 4px;
 text-align: center;
 position:relative;
}

JS:

$('div').draggable({
 cancel: "ta",
 start: function (){
    $('#ta').focus();
 } ,
 stop: function (){
    $('#ta').focus();
 } 
});
like image 182
Crazy Versatile Avatar answered Nov 10 '22 00:11

Crazy Versatile