Do not allow an incorrect item to be sorted in a sortable list

I have attempted various strategies, but unfortunately without success. My current challenge involves two sortable lists that are interconnected. List 'A' is configured to accept any list item, while list 'B' will only receive items with the class 'abc.'

The HTML code snippet is shown below:

<ul id='A'>
  <li>item A1</i>
  <li>item A2</i>
  <li class='abc'>item A3</i>
</ul>

<ul id='B'>
  <li class='abc'>item A1</i>
</ul>

My jquery implementation is as follows:

$('#A').sortable({revert: true, connectWith: '#B'})
$('#B').sortable({revert: true, connectWith: '#A', over: function(event, ui){
   if(!ui.item.hasClass('abc')){
     ui.placeholder.addClass('ui-state-error');
     ui.sender.sortable('cancel');
   }
}})

I would appreciate any guidance on where I may be going wrong. Thank you.

Answer №1

If you're looking for an alternative, consider using the receive event instead. Keep in mind that there may be a slight delay when doing so and the addClass function might not work as expected:

$('#A').sortable({revert: true, connectWith: '#B'})
$('#B').sortable({revert: true, connectWith: '#A',
    receive: function(event, ui){
        if(!ui.item.hasClass('abc')){   
            $(ui.placeholder).addClass('ui-state-error');                    
            $(ui.sender).sortable('cancel');
        }}
})​;​

Check out this example - http://jsfiddle.net/b5ykK/1/

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

What could be causing the disappearance of my <Div> when applying a Class to the contained <span>?

My goal is to showcase a variable on my HTML page using CSS for a Graphical display in OBS streaming software. Whenever I apply the class .wrappers to the span id p1Name (and p2Name), the text disappears from the HTML output in OBS. I'm puzzled as to ...

Tips for personalizing Bootstrap columns using identical class names?

Is there a way to customize the background color of each individual .col-5 within the same row while using Bootstrap and a custom CSS file? Changing the styles for one column seems to affect all columns with the same class name. Edit: I'm looking f ...

Changing the background image within a CSS class on an imported Vue component

Is there a way to dynamically change the background-image on a CSS class within an imported component? I have successfully installed 'vue-range-slider' and imported RangeSlider. The setup for the range-slider is as follows: <template> ...

What is the best way to update a specific column value in a table using AngularJS?

How can I achieve opening a specific row's text-area when clicking the "Add_Note" button inside a table column, without affecting all other rows? I want to show them using index value while using ng-repeat. var myApp = angular.module('myApp&ap ...

Troubleshooting Problems with Fancybox Scaling on Small Landscape-Oriented Mobile Devices

Issue Description: In my responsive web design, I am utilizing Fancybox v2.1.5. The problem arises when smaller mobile devices with a more rectangular shape than square are used. In portrait orientation, when invoking Fancybox, it expands to the width of ...

Issue with AJAX function not initiating PHP script upon subsequent button clicks

My button has the following function: $(function() { $("#submit").click(function(){ $.ajaxSetup({ cache: false }); $.ajax({ url:'crfile.php', type:'GET', }); }); }; When clicked ...

Guide on configuring href tags in single-page JavaScript/HTML5 apps

After developing a "small" single page web application using python/django/backbone, similar to a basic Todo List app, a challenge arose regarding maintaining SEO-friendly href attributes without forcing users' browsers to reload the entire app at a n ...

Creating an Interactive Countdown Timer with JavaScript

I am currently working on a website that includes a modified version of a JavaScript countdown element. While I have been learning a lot about flex grids, I haven't delved much into block/inline-block layouts. One issue I'm facing is that when t ...

Does MySQL Workbench automatically convert camel case attributes?

I'm curious to understand something. I defined an attribute called userId, but in MySQL Workbench it shows up as user_id. Is this usual? @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name= "userId") private ...

Responsive design with Flexbox, interactive graphics using canvas, and dynamic content resizing

I'm having difficulties with my canvas-based application. I have multiple canvases, each wrapped in a div container alongside other content. These containers are then wrapped in an "hbox" container. The objective is to create a dynamic grid of canvas ...

Guide to attaching a page content button to a fixed header

I am currently developing a webpage with a sticky header, and I want to include an animated button that moves onto the header when scrolled past. You can check out my code snippet here: https://jsfiddle.net/ykL50pjf/ The specific ID for the button is #t ...

Change the input field font style in AngularJS

Check out this Plunker link for validation of input field: http://plnkr.co/edit/iFnjcq?p=preview The validation only allows numbers to be entered and automatically adds commas. My query is, if a negative number is entered in the field, how can I change th ...

How to keep text always locked to the front layer in fabric.js without constantly bringing it to the front

Is it possible to achieve this functionality without using the following methods? canvas.sendBackwards(myObject) canvas.sendToBack(myObject) I am looking to upload multiple images while allowing them to be arranged forward and backward relative to each o ...

Display only the labels of percentages that exceed 5% on the pie chart using Chart JS

I'm currently diving into web development along with learning Chart.js and pie charts. In my project, the pie chart I've created displays smaller percentage values that are hardly visible, resulting in a poor user experience on our website. How c ...

I am looking to continuously update the progress bar as the AJAX responses are being received

In order to update the progress bar with every response individually, I encountered an issue where multiple responses were being received at once and only the last one would update the progress bar. Despite trying async:false, the progress bar would only u ...

Images that adapt to various sizes while maintaining a consistent visual hierarchy

Hey there, I'm struggling to achieve the same row size of images as shown in the example. However, when I adjust the window size, the larger images become smaller than the columns on the left. This has been a challenge for me and I haven't found ...

What is the best way to send a value using onClick and navigate to a page using href in jquery?

How can I load a page through href and send data using onClick function? Here is the code I have: <a href='doc-menu-2.html' onClick='doc_menu_2(10)'>Item Name</a> <script> myApp.onPageInit('doc-menu-2', fun ...

Get the value from AJAX and input it into the text box through PHP

I'm attempting to retrieve database results to check the availability of a username, but I'm not receiving any Ajax response. Below is the HTML code: <form id="user_form"> <input placeholder="Enter username" type="text" name="aj ...

When I try to share a link to my website on Google Plus, the thumbnail does not appear

Previously, I encountered a similar issue on Facebook, but by utilizing the following tag, I was able to resolve it: <meta property="og:image" content="link to image" /> The dimensions of the image in the "link to image" are 200px x 200px, which me ...

IE does not support Overflow and Ellipsis in this table

FF v14 has the exact appearance I want. Unfortunately, IE9 is not cooperating as it does not hide overflow or display the ellipsis. Does anyone know how to resolve this issue for compatibility with IE9? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tran ...