JQuery droppable, when list item is dropped, unable to get 'id' attribute using attr() - jquery-ui

My problem is similar to the below link
How to get the dropped item's id on drop event jquery
However I am dragging and dropping a list item, each contain images.
When the list item is dropped, I need to get the 'id' attribute, which will then be passed to php on 'submit', to update the database.
But the below code does not seem to be capturing the 'id' attribute or if i try 'src', I have amended the following
$("#drop").text(id); to $("#drop").text("image has been dropped");
which does display correctly, "image has been dropped" upon a drop event, so the problem seems to be with getting the 'id' attribute within a list item? I tried adding a class called 'image' to use instead of the list item, tried many things, but makes no difference!
Hope someone can point me in the right direction!
Thanks
below is my code
JQuery
$(document).ready(function() {
$("#item-slider li").draggable({
containment: 'document',
revert: true,
helper: "clone",
cursor: "move"
});
$("#drop").droppable({
accept: "#item-slider li",
drop: function (event, ui){
var draggable = ui.draggable;
var id = draggable.attr("id");
$("#drop").text(id);
}
});
});
Html
<div id="item-slider">
<ul>
<li class='image'><img src="./images/jeans1.jpeg" id="jeans1.jpeg" /></li>
<li class='image'><img src="./images/top2.jpg" id="top2.jpg" /></li>
<li class='image'><img src="./images/top1.jpg" id="top1.jpg" /></li>
<li class='image'><img src="./images/shoes3.jpg" id="shoes3.jpg" /></li>
<li class='image'><img src="./images/dress1.jpg" id="dress1.jpg" /></li>
</ul>
<div id="drop">
</div>
</div>

The id attributes you're looking for do not belong to your <div> elements, but to their <img> children. You can match these elements with find() or children():
drop: function(event, ui) {
var id = ui.draggable.find("img").attr("id");
$("#drop").text(id);
}

Related

disable an item in sortable when it is in correct predefine position

I am using sortable() in a game in which the student has to sort items.
When an item is in its correct position, I want them to be disabled.
So far I have been able to give the items a border color but I am not able to disable them so the student cannot drag them again.
see here.
then when all the items are in the correct position, a feedback alerts the user.
This is similar: jquery ui sortable disable for one li item
Attempted Example: https://jsfiddle.net/Twisty/dujfwLfz/
HTML
<ul id="sortable">
<li id="log-1" class="sort"><img src="https://dummyimage.com/190x30/914848/ffffff&text=If+It" class="default" /></li>
<li id="log-2" class="sort"><img src="https://dummyimage.com/190x30/914848/ffffff&text=Too+Heavy!" class="default" /></li>
<li id="log-3" class="sort"><img src="https://dummyimage.com/190x30/914848/ffffff&text=Than+pedal+of+flower+resting" class="default" /></li>
<li id="log-4" class="sort"><img src="https://dummyimage.com/190x30/914848/ffffff&text=On+grass,+oh+still+too+heavy+it+were," class="default" /></li>
<li id="log-5" class="sort"><img src="https://dummyimage.com/190x30/914848/ffffff&text=were+lighter+touch" class="default" /></li>
</ul>
JavaScript
$(function() {
var final = [
1,
5,
3,
4,
2
];
$("#sortable").sortable({
items: "li:not(.done)",
update: function(e, ui) {
var currentOrder = [];
$(this).find("li").each(function(ind, elem) {
var itemId = $(elem).attr("id");
currentOrder.push({
id: itemId,
ordNum: itemId.slice(-1)
});
});
console.log(currentOrder);
$.each(final, function(k, i) {
if (currentOrder[k].ordNum == i) {
// Match
$("#" + currentOrder[k].id).removeClass("sort").addClass("done");
}
});
$(this).sortable("refresh");
}
})
.disableSelection();
});
Changing the class allows us to no longer sort the items.

Combinate JQuery UI sortable and draggable with Bootstrap Tab navigation

I try build page based on Bootstrap with JQuery UI sortable list.
I have one list in global space and two lists in Tabs
and I like to drag items from external list and drop it to tab navigation for adding into list
However when I switch to bootstrap tab navigation the external item on drop disappear but doesn't appear in tab list
this is my example based on JQ UI "Connect list with Tab"
This is my JS FIDDLE
$(function() {
$( "#sortable0, #sortable1, #sortable2" ).sortable().disableSelection();
var $tabs = $( "#tabs" );//.tabs();
$('#myTab a:last').tab('show');
var $tab_items = $( "ul:first li", $tabs ).droppable({
accept: ".connectedSortable li",
hoverClass: "ui-state-hover",
drop: function( event, ui ) {
var $item = $( this );
var $list = $( $item.find( "a" ).attr( "href" ) )
.find( ".connectedSortable" );
ui.draggable.hide( "slow", function() {
$tabs.tabs( "option", "active", $tab_items.index( $item ) );
$( this ).appendTo( $list ).show( "slow" );
});
}
});
});
HTML
<div id="external-tab">
<ul id="sortable0" class="connectedSortable ui-helper-reset">
<li class="ui-state-default">ExItem 1</li>
<li class="ui-state-default">ExItem 2</li>
<li class="ui-state-default">ExItem 3</li>
</ul>
</div>
<div id="tabs" class="tabbable">
<ul id="myTab" class="nav nav-tabs">
<li>Nunc tincidunt</li>
<li>Proin dolor</li>
</ul>
<div class="tab-content">
<div id="tabs-1" class="tab-pane">
<ul id="sortable1" class="connectedSortable ui-helper-reset">
<li class="ui-state-default">Item 1</li>
<li class="ui-state-default">Item 2</li>
<li class="ui-state-default">Item 3</li>
</ul>
</div>
<div id="tabs-2" class="tab-pane">
<ul id="sortable2" class="connectedSortable ui-helper-reset">
<li class="ui-state-highlight">Item 1</li>
<li class="ui-state-highlight">Item 2</li>
<li class="ui-state-highlight">Item 3</li>
</ul>
</div>
</div>
</div>
It's a shame nobody else got to your question in the past year... I'm guessing this is no longer relevant for you personally, but maybe we can help to resolve this issue for any other user searching on Google for solutions to include jQuery UI's API for Draggable/Droppable items, integrated with Bootstrap's tabbed component.
If I'm reading your question correctly, you'd like to be able to drag items from the external list into either of the two tabs. If your problem is similar to the one I'm personally tackling at the moment, you might also want to drag items from tab one into tab two, and vice-versa. I've updated your fiddle with a solution that accommodates both of these needs. The lists are also sortable, as they were previously, and the classes of items dragged from one space to the other are retained as well.
Items I might consider adding in the future are:
The ability to drag not only onto the tab, but also the space below it
Adding all attributes from the parent element dragged, not just the class list
However, for the moment, all I've done is to rectify the solution you posted.
The updated fiddle is here: http://jsfiddle.net/cr8s/96dsB/30/
Also, since the Internet is a fickle place and things disappear all the time, here's the updated JS (I left your HTML unchanged, and that's already in the question above):
$(function() {
$('.tab-pane > ul').sortable().disableSelection();
var
$tabs = $('#tabs'),
$tabItems = $('#myTab > li', $tabs);
$('#myTab a:last').tab('show');
$tabItems.droppable({
accept: ".connectedSortable li",
hoverClass: "ui-state-hover",
drop: function(event, ui) {
var
$item = $(this),
href = $item.children('a').attr('href'),
$list = $(href).find('.connectedSortable');
console.log($list);
$list.append($('<li />').html(ui.draggable.html()).attr('class', ui.draggable.attr('class')));
$list.closest('.tab-pane').addClass('active').siblings().removeClass('active');
$item.addClass('active').siblings().removeClass('active');
}// drop(event, ui)
});// $tabItems.droppable()
$('#external-tab > ul > li').draggable({
appendTo: 'body',
helper: 'clone'
});
});// end of doc ready
Hope that solves this problem for some folks! It certainly gets me closer to solving my own use case, and I'll likely continue to update the fiddle linked above as I add additional features like the two I mentioned earlier.

how do I access a parent attribute of a selected line item in a jquery ui menu

On my html page, I have multiple menus. When a line item is clicked, I want to get the id attribute of the parent anchor tag from the menu where the selection originates. My html and jquery code is below.
//HTML
<ul id="menuA">
<li>
Age Group
<ul>
<li name="ageGroup">18-21</li>
<li name="ageGroup">21-30</li>
<li name="ageGroup">30-40</li>
<li name="ageGroup">40-50</li>
<li name="ageGroup">50-60</li>
<li name="ageGroup">60-70</li>
<li name="ageGroup">70-80</li>
<li name="ageGroup">80-90</li>
<li name="ageGroup">90-100</li>
</ul>
</li>
</ul>
//JQUERY
$(function(){
$('#menuA').menu({
select: function(event, ui){
var choice = ui.item.text(); //get text of menu selection
//here is where I want to get the id attribute of the anchor tag. What I have isn't working. id is coming up undefined.
var id = ui.item.parents('a').attr('id');
}
});
You will find the corresponding tag id by using:
var id = ui.item.parents('li').find('a').attr('id');
Your previous attempt didn't work as parent selector was returning your list items () instead of searching for a tag in the main li.

jquery ui sortable, div block in first item not visible

I want to make the div - "small_box" in the first item of sortable list to be always not visible. I tried with jquery first() but that works only once and only for one and same element whenever it's dragged. How can i simple make it back to be visible when I dragg it into other than first place and then make invisible "small_box" for the item which jumps in the first place?
I put the live example here: http://jsfiddle.net/kriskasper/3knnn/
<ul id="sortable" class='connectedSortable'>
<li>
<div class="small_box">small box</div>
<div class="big_box">big box</div>
</li>
<li>
<div class="small_box">small box</div>
<div class="big_box">big box</div>
</li>
<li>
<div class="small_box">small box</div>
<div class="big_box">big box</div>
</li>
<li>
<div class="small_box">small box</div>
<div class="big_box">big box</div>
</li>
</ul>
And here is jquery ui function:
$(function() {
$( "#sortable" ).sortable({
connectWith: ".connectedSortable",
placeholder: "ui-state-highlight",
forcePlaceholderSize: true,
opacity: 0.6,
revert: 70
});
});
Please help.
You should play with the sortable plugin events. The sortreceive event is fired when the connected element receives an item. My guess would be something like ...
$("#sortable").bind("sortreceive", function(event, ui) {
$(".connectedSortable .small_box").show();
$("#sortable .small_box").first().hide();
});

How to get an object of the new element, which was placed instead of previous object in jQuery UI sortable?

The source:
<ul class="supercategory">
<li>
<div class="supcat">
<h4>
<a class="new" href="/header1.html">header 1</a>
</h4>
<ul class="category">
<li>Item 1</li>
<li>Item 2</li>
</ul>
</div>
</li>
<li style="" class="">
<div class="supcat">
<h4>
<a class="new" href="/header2.html">Header 2</a>
</h4>
<ul class="category">
<li>Item 1</li>
<li>Item 2</li>
</ul>
</div>
</li>
</ul>
<div id="test">
</div>
a lit bit of css:
ul.supercategory { background: lightblue; }
.new{ background: yellow; }
and a jQuery UI force:
$('.supercategory').sortable({
cursor: 'move',
axis: 'y',
revert: true,
stop: function (event, ui) { }
});
I know that to get an object of the current dragged element I simply must to make something this:
stop: function(event, ui) { var obj = ui.item; }
But how to get an object of placed element instead of I dragged to new position?
For example, if I dragged first li on the new position (it will 2nd li) the second li was placed on the place instead of my, how to get this element as object in jQuery UI sortable?
Watch my fiddle: http://jsfiddle.net/Stasonix/jCMuQ/1/
upd 1. First of all a fiddle. http://jsfiddle.net/Stasonix/jCMuQ/5/
Than code:
var is_received = false;
$('.supercategory').sortable({
cursor: 'move',
axis: 'y',
revert: true,
receive: function(event, ui){ is_received = true; },
stop: function (event, ui) {
is_received = true;
},
deactivate: function(event, ui) {
if (is_received){
$('#test').text(ui.item.find('.new').attr('href'));
is_received=false;
}
}
});
Well... still need a solution, it's not works seems.
You can try this to get the new object you dragged by keeping track of your object's index / position, and by using the events start and deactivate like this:
// Define static variables to keep track of the index / position of your dragged object
var prev_index = null; // Previous index / position of your dragged object
var next_index = null; // Next index / position of your dragged object
$('.supercategory').sortable({
cursor: 'move',
axis: 'y',
revert: true,
start : function(event, ui) {
prev_pos = ui.item.index();
},
deactivate : function(event, ui) {
next_pos = ui.item.index();
},
stop : function(event, ui) {
// Your new object: "el"
var el = null;
if(next_pos > prev_pos)
el = $(".mydrag").get(next_pos-1); // Your previous object has been dragged! Your previous object was initially above your new object (Dragging downward)
else if(next_pos<prev_pos)
el = $(".mydrag").get(next_pos+1); // Your previous object has been dragged! Your previous object was initially below your new object (Dragging upward)
else
el = $(".mydrag").get(prev_pos); // Your previous object was not dragged and is at the same position
$('#test').text($(el).find('.new').attr('href'));
}
});
The thing is,
if your object was dragged successfully, depending on whether you drag your object downward or upward, the new object you're looking for will be located above or below the dragged object, so: at the new index / position of your dragged object -1 / +1
however, if your object was not dragged successfully, there is "no new object", so your new object is your dragged object.
Make sure to add the class .mydrag to your draggable li elements:
<ul class="supercategory">
<li class="mydrag">
<div class="supcat">
<h4>
<a class="new" href="/header1.html">header 1</a>
</h4>
<ul class="category">
<li>Item 1</li>
<li>Item 2</li>
</ul>
</div>
</li>
<li class="mydrag">
<div class="supcat">
<h4>
<a class="new" href="/header2.html">Header 2</a>
</h4>
<ul class="category">
<li>Item 1</li>
<li>Item 2</li>
</ul>
</div>
</li>
</ul>
<div id="test">
</div>
I'm not sure if this solution is what you were exactly looking for...
You can ask me question if I was not clear.

Resources