I'm new with jQuery. All I want is to make sortable. When something is dropped, it hides and open textarea with save button. When the save button is clicked text from textarea writes to a paragraph.
I searched a lot and I found these fiddles:
First one
Second one
I want to 'merge' their functionality, but when I try it's not working. Here is my code:
$('#sortable').sortable().droppable({
drop: function(ev, ui){
$(ui.draggable).html('<a id="send-thoughts" href="#">Click</a><textarea name="message"></textarea>');
}
});
$('#draggables li').draggable({
connectToSortable: '#sortable',
helper: 'clone',
revert: 'invalid',
cursor: 'move'
});
$('#send-thoughts').click(function()
{ var thought = $('textarea[name=message]').val();
alert(thought);
});
Here is a fiddle with my work - jsfiddle.net/CxpMn/102/ (sorry, I need more reputation to post more links). Please, help!
Several issues:
First, IDs cannot be duplicated in a page, so you need to use a class instead for #send-thoughts.
Second, you can't assign a click handler to elements that don't exist yet; so we need to delegate that event handler to an element that does exist, and target the future elements within it.
Third, we need to target the textarea that is related to the element clicked
$('#sortable').sortable().droppable({
drop: function(ev, ui){
// use class instead of ID
$(ui.draggable).html('<a class="send-thoughts" href="#">Click</a><textarea name="message"></textarea>');
}
});
// delegate event to account for future elements
$(document).on('click','.send-thoughts',function() {
// get the nearest textarea
var thought = $(this).siblings('textarea[name=message]').val();
alert(thought);
});
DEMO
The problem is that you are attaching a click handler to #send-thoughts before it is ever added to the DOM. Try something like the following:
$('#sortable').sortable().droppable({
drop: function(ev, ui){
$('#my-container').show();
}
});
$('#draggables li').draggable({
connectToSortable: '#sortable',
helper: 'clone',
revert: 'invalid',
cursor: 'move'
});
$('#send-thoughts').click(function()
{ var thought = $('textarea[name=message]').val();
alert(thought);
});
In your HTML put:
<div id="my-container" style="display: none">
<a id="send-thoughts" href="">Click</a><textarea name="message"></textarea>
</div>
Related
I have Jquery code to drag and drop elements to a work_area div with works perfectly fine.
But when I try to apply the same function to a preloaded DOM element the draggable function using another draggable() function on document ready, the preloaded elements are draggable within the work area but the new elements are not.
For Better understanding here's the code:
Code to drag & drop new elements:
function dragger() {
$(".draggable").draggable({
revert: "invalid",
containment: "#work_area_div_sub",
helper: "clone",
cursor: "move"
});
$(".draggable2").draggable({
revert: "invalid",
containment: "#work_area_div_sub",
cursor: "move"
});
console.log("here2");
$("#work_area_div_sub").droppable({
accept: ".draggable",
containment: "#work_area_div_sub",
drop: function( event, ui ) {
console.log("here3");
ui.draggable.clone().appendTo($(this)).draggable().removeClass("draggable").addClass("draggable2");
$(".draggable2").resizable({
handles: "n, e, s, w, se, ne, sw, nw",
aspectRatio: true
});
});
}
$(document).ready(function() {
dragger();
});
Please note that the above code lets drag and drop (clones) elements with class name draggable into work_area div, which further can be draggable within the work_area div.
And works perfectly fine.
While, when I apply the same to the preloaded elements with class draggable2 using the below code:
function dragger_idle() {
$(".draggable2").draggable({
revert: "invalid",
containment: "#work_area_div_sub",
cursor: "move"
});
$("#work_area_div_sub").droppable({
accept: ".draggable2",
containment: "#work_area_div_sub",
drop: function( event, ui ) {
console.log("here");
}
});
}
$(document).ready(function() {
dragger_idle();
});
works fine, lets me drag pre-loaded elements that are in work_area div drag around. but the first piece of code which lets me drag and drop new elements doesn't work. But if i m to remove the dragger_idle() the dragger() function executes fully.
I tried condole.log on every possible position to check how far the function is running. yet, no idea where I messed up.
At first I thought that the $("#work_area_div_sub").droppable in draggable_idle() is messing up with $("#work_area_div_sub").droppable in draggable() but consoling it out tells me that the $("#work_area_div_sub").droppable is not at all triggering when i try to drag new elements.
Hope I was clear.
Any Help is greatly appreciated.
So, after some brain storming, I figured it out.
function dragger() {
$(".draggable").draggable({
revert: "invalid",
containment: "#work_area_div_sub",
helper: "clone",
cursor: "move"
});
$(".draggable2").draggable({
revert: "invalid",
containment: "#work_area_div_sub",
cursor: "move"
});
$("#work_area_div_sub").droppable({
accept: ".draggable, .draggable2",
containment: "#work_area_div_sub",
drop: function( event, ui ) {
if((ui.draggable).hasClass('draggable2')) { } else {
//whatever we wanna do with the new drag drop elements
}
}
});
}
So, what I exactly did was,
told the #work_area_div_sub div to accept both classes draggable and draggable2
And while adding the clone or other functions to the dropped element, added an if to check whether it's a draggable or the other.
and finally added parameters accordingly.
To be frank, although no answers rolled in, stack really did help me understand my own problem in a different way to solve it by myself by writing it down. So. thanks SO.
I am implementing drag and sort function. i have 2 Draggable area. you can take a look at URL
https://study-navin10sharma.c9.io/
1.Just take city as example and drop into active reason it work and then take again city out of Active reason still everything fine. But again if you try to add city in Active reason then city block will be invisible.
With each drag i perform sort operation dynamically.
$(function() {
$( "#sortable1, #sortable2" ).sortable({
connectWith: ".connectedSortable",
helper: 'clone',
containment:"document",
revert:"true",
tolerance:"pointer"
});
});
$("#sortable2").sortable({
stop: function ( event, div){
data = $(this).sortable('toArray', { attribute: 'data-id' });
//Here we perform our ajax request
},
receive: function (event, ul) {
ul.item.remove();
}
});
//WITH EACH DRAG it connect with sort
$("#sortable1>div").draggable({
connectToSortable: "#sortable2",
helper: "clone",
revert: "invalid",
axis :"y"
});
$("#sortable2>div").draggable({
connectToSortable: "#sortable2",
helper: "clone",
revert: "invalid"
});
//HERE SOME HTML REFERENCE
<div id="sortable2" class="connectedSortable"> <span class="ui-sortable-handle"> ACTIVE</span></div>
<div id="sortable1" class="connectedSortable">
// Here i have list of the element that i want to Drag
</div>
One more things currently once i drag the item sorting trigger but this not a good way may be once element drop then sorting need to be start.
Please help me to sort out this problem. if there anything else you want let me know.
Thanks.
I am trying to use the jquery-ui draggable to make some element draggable.
I set the helper option to clone the current element.
It's making the clone correctly but when I drop the clone disappears. It doesn't stay at the dragged place.
See this for Demo Fiddle Link
$('#drag').draggable({
helper: function (e, ui) {
return $(this).clone();
}
});
What am I missing ?
There maybe a simpler way, but through data of draggable, you can target a property that deals with this. Like this:
stop : function(e, ui){
$('#drag').draggable().data()["ui-draggable"].cancelHelperRemoval = true;
}
fiddle: http://jsfiddle.net/n10ucrLd/
I think there's been a lot of troubles with helper: 'clone'. I always got it to work, when I defined a droppable as well. E.g.:
HTML:
<div id="drag">Drag This</div>
<div class="container"></div>
JavScript:
$('#drag').draggable({
helper: function (e, ui) {
return $(this).clone(true);
}
});
$( ".container" ).droppable({
drop: function (event, ui) {
ui.draggable.clone().appendTo($(this)).draggable();
}
});
Live example: http://jsbin.com/vibeqaganu/1/edit?html,css,js,output
I'm now developing a website that can drag elements and drop into a table cells (each cell has its "ID")
Now I can drag and drop the element to the cells but I want to know
How to get the ID of table cell where I dropped the element?
here's the picture's concept.
Suppose that cell's ID are the combination of row and column number.
At first you need to rename yours IDs because one ID can't start with a number. You neet at lest one letter, example "ID11" "ID12" ...
Than what are the Cells? div? Td? Input?
Can you a jsfiddle ?
-Edit.
you need something like thise:
jsfiddle
you must only change your skript to:
$(function() {
$("#draggable").draggable({ snap: ".drop"});
$(".drop").droppable({
drop: function(event, ui){
console.log("Dropped to" + $(".drop").attr("id"));
$( this )
.addClass( "ui-state-highlight" )
.find( "p" )
.html( "Dropped!" );
}
});
});
Here is the working fiddle.
Try something like this:
$(function() {
$("#draggable").draggable({ snap: ".drop"});
$(".drop").droppable({
drop: function(event, ui){
console.log($(this).attr('id'));
}
});
});
Check this fiddle.
First you need to enable the table cells to be dragged to by defining ondragover event callback. In the fiddle:
box.ondragover = function (evt) { evt.preventDefault(); };
preventDefault ensures that the browser does not set a dropdown lock on the element.
Then you simply play around with the event data, like this:
box.ondrop = function (evt) { droppedTo.innerHTML = evt.toElement.id; };
toElement holds reference to the element that you drop on, and you can get all the data you need from it, including id.
You need to change your script like this
$(function () {
$("#draggable").draggable({
snap: ".drop"
});
$(".drop").droppable({
drop: function (event, ui) {
alert($(this).attr("id"));
console.log("Dropped to " + $(this).attr("id"));
}
});
});
Change from $(".drop").attr("id") to $(this).attr("id") to get the id of the element that you are dropping into :)
Example http://jsfiddle.net/81ja79Ls/4/
I have some code in a jsfiddle and I want to display one hidden element <div id="e1"></div>
I have written code for this in jquery like this:
$("#credit4").sortable({
receive: function (event, ui) {
ui.item.remove();
var s="PLEASE SELECT ANOTHER BLOCK";
$("#e1").show();
$("#e1").html(s);
setTimeout('$("#e1").hide()',1500);
}
});
credit4 is the id of a draggable element and when the user wants to drag the element then this hidden elemet should be displayed.
You can also check my jsfiddle here - http://jsfiddle.net/sanjayrathod7/5cZD5/44/.
Please suggest me where I'm wrong.
Here's a simplified fiddle with a set of alerts in place indicating which line is hiding #e1. You can see that you're probably hiding it when you didn't intend to (at lines 45 and 171):
http://jsfiddle.net/isherwood/5cZD5/50
$("#credit").sortable({
receive: function (event, ui) {
ui.item.remove();
$("#e1").show();
$("#e1").html(s);
setTimeout('$("#e1").hide()', 1500); alert('hiding 10');
}
});
I have find out the answer
Try this
$( "#credit4" ).draggable({
revert: true,
start: function( event, ui ) {
var s="PLEASE SELECT ANOTHER BLOCK";
$("#e1").show();
$("#e1").html(s);
setTimeout('$("#e1").hide()',1500);
}
});