Tips for altering the appearance of a dropped item using JQueryUI sortable

I have a straightforward website where I need to implement the ability to drag and drop styled DIV elements between two containers. Utilizing JQueryUI's sortable function, this behavior was successfully achieved with the following code:

$("#active-container").sortable({connectWith: "#inactive-container"});
$("#inactive-container").sortable({connectWith: "#active-container"});

To add droppable DIV elements to the containers upon button click, I use the createDeviceDiv function like so:

function createDeviceDiv (deviceObject, className){

  var div = document.createElement('div');

  div.id = deviceObject.deviceId;

  div.className = className + ' draggable="true"';

  div.textContent = deviceObject.deviceName;

  return div;
}

The CSS for the elements is defined as follows:

.active-device-element
{
  width: 200px;
  height: 40px;
  border: 2px solid #666666;
  margin: 10px;
  background-color: lightgreen;
  cursor: move;
  border-radius: 25px;
  text-align: center;
  vertical-align: middle;
  line-height: 35px; 
  -webkit-user-select: none; 
}

.inactive-device-element
{
  width: 200px;
  height: 40px;
  margin: 10px;
  border: 2px solid #666666;
  background-color: floralwhite;
  cursor: move;
  border-radius: 25px;
  text-align: center;
  vertical-align: middle;
  line-height: 35px; 
  -webkit-user-select: none; 
}

#active-container, #inactive-container
{
  background-color: lightgrey;
  min-height: 100px;
}

The corresponding part of the index.html page (using Twitter bootstrap 3) looks something like this:

<div class="row">
    <div class="col-xs-6">
        <h2>Active Devices</h2>
        <p>The following devices are currently listed as active.</p>
        <div id="active-container">
        </div>
    </div>
    <div class="col-xs-6">
        <h2>Inactive Devices</h2>
        <p>The following devices are currently listed as inactive.</p>
        <div id="inactive-container">
        </div>
    </div>
</div>

Even though dragging and dropping between containers functions properly, the dropped element retains its original style. To indicate modifications by changing the background color or style, perhaps to '.modified-device-element', guidance on the best approach would be greatly appreciated.

Any assistance provided will be immensely helpful. Thank you.

Answer №1

For those utilizing jQuery UI Sortable, there are numerous events available for implementation, such as the receive event. By setting up an event listener, updates from both containers can be monitored. This allows for tracking items moving between containers and modifying them accordingly by adding a new class.

$('#active-container, #inactive-container').on('sortreceive', function(event, ui) {
    ui.item.addClass('modified-device-element');
});

An alternative approach is to incorporate the event during widget setup:

$('#active-container').sortable({
    receive: function(event, ui) {
        ui.item.addClass('modified-device-element');
    }
});

$('#inactive-container').sortable({
    receive: function(event, ui) {
        ui.item.addClass('modified-device-element');
    }
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Insert a new, unique div onto an HTML webpage using JavaScript, ensuring no duplicates are created

Having an issue with this code snippet where nothing is being added. It seems like the variable result may not be taking a number or something else, but I'm not entirely sure why. $(document).ready(function () //document.getElementById(1).style. ...

Tips for deleting part of the URL at the end of all hyperlinks on a webpage

I have a large number of links on the same page that I would like to clean up by removing everything from specific characters until the end of the URL. Currently, there are too many links like this scattered throughout my page. The string "$amp;rs" or "&a ...

Issue with the back-to-top button arises when smooth-scrolling feature is activated

This Back To Top Button code that I discovered online is quite effective on my website. // Defining a variable for the button element. const scrollToTopButton = document.getElementById('js-top'); // Creating a function to display our scroll-to- ...

Issues with routeparams are preventing ng-repeat from functioning properly, without any response or resolution

For my shoppingCart project, I am working on dynamically bringing data into views. I am using routeParams in template.html but facing an issue. The data is arriving normally as checked via ng-href="#/store/{{something.name}}/{{ child.name }}" but it isn&ap ...

I'm encountering a Parse error: syntax error, wherein the file unexpectedly reaches its end

I'm currently in the process of creating a personalized search engine that enables users to search using multiple fields. However, an error message stating "syntax error, unexpected end of file" keeps popping up, specifically on line 201 of my code, r ...

What could be the reason for the Express function Router() returning a value of undefined?

Currently, I am working with TypeScript and Express to develop an API that adheres to the principles of Clean Architecture. To organize my application, I have structured each route in separate folders and then imported them all into an index.ts file where ...

I created a custom discord.js-commando command to announce all the channels that my bot is currently active in, however, encountered an unexpected error

const Commando = require('discord.js-commando'); module.exports = class AnnounceCommand extends Commando.Command { constructor(client) { super(client, { name: 'announce', aliases: ['an'], ...

What could be the reason why the AngularJS form is set to read-only

Explaining the setup of my app in HTML: <div ng-app="md-app"> <div id="general"> <ng-include src="template1" ng-controller="GeneralCtrl"></ng-include> </div> </div> The JavaScript function to fetch a pe ...

What steps should I take to handle an ArrayIndex out of bound error in JavaScript?

Consider the following array: var john = ['asas','gggg','ggg']; If an attempt is made to access john at index 3, i.e. john[3], it results in an error. Is there a way to show a message or trigger an alert indicating that ther ...

The issue of empty req.body in POST middleware with Medusa.JS

Feeling completely frustrated with this issue. Grateful in advance to anyone who can lend a hand. Any insights on why req.body is showing up empty? Medusa.JS should be utilizing bodyParser by default, correct? It was functioning fine earlier today but now ...

Is it possible to incorporate HTML and CSS into Kivy and Python 3 development?

Currently in the process of creating a multi-touch kivy program using Python 3.52 on Linux. While Kivy is effective, I've encountered challenges with GUI development and experienced laggy animations. I've noticed that my program tends to slow do ...

Convert a rendered Django template into Json and include additional elements

Imagine a scenario where there is a view connected to a template, passing some context data (i.e. objects) to be rendered in the HTML output displayed in the browser. The typical view setup would look something like this: # views.py def view_name(request ...

The `$scope variable fails to update in another controller`

I am currently facing an issue with updating a value on my view. Let me walk you through my code along with a brief explanation of the situation. The code may look messy as I have been experimenting with different combinations lately. The controller in qu ...

Stylesheet for a React component

Why is the CSS code in my React component file not working even though I have imported it? import React from 'react'; import { Carousel } from 'react-bootstrap'; import './Banner'; ...

Implement jQuery to toggle a class on click for added functionality

I am attempting to create a box that changes color when clicked. When the box is first clicked, it will turn red by adding the class red, and if clicked again, it will change to blue. The colors alternate with each click, but I am unsure of how to achieve ...

"Enable a smooth transition and switch the visibility of a div element when clicked

How to create a dropdown form with a transition effect that triggers on click of an element? Once triggered, the transition works smoothly but clicking again only hides the div element without triggering the transition. See the demo here: Check out the fu ...

Styling elements with CSS

I've been attempting to align a button to the right of another button. The example above illustrates what I'm trying to achieve. I used Bootstrap to build it, which has a useful navbar feature for layout. However, despite my efforts to use right ...

Creating a string-based template that can be included in Django

I need to create a list of clickable links for my navigation bar with the URLs provided using the url-tag. To achieve this, I have a list of dictionaries containing all the necessary information for each link, and I am creating a template string with them ...

Sending a document to Sharepoint through webservices seamlessly with Jquery, all without the need for a page reload

Currently, I am experimenting with implementing a wizard interface using jquery where the user can input information into a dialog box and click 'next' to proceed to the next step without refreshing the page. As part of this process, I also want ...

Meteor - An error occurred in the Subscription Publish Function because it returned an array of non-Cursors

I'm attempting to publish a collection, but my console is showing that it returns an array. server/publish.js HeartCount = new Mongo.Collection('heartcount'); Meteor.publish("currentHeartCount", function() { return HeartCount.find().f ...