Moveable elements can be sorted into a grid format

Trying to implement a draggable list that can be sorted using jQuery has been successful so far. However, when attempting to convert this list into a grid layout using CSS, the functionality of dragging and sorting stops working.

The HTML code is as follows:

<ul id="sortable2">
  <li class="ui-state-highlight">A</li>
  <li class="ui-state-highlight">B</li>
  <li class="ui-state-highlight">C</li>
  <li class="ui-state-highlight">D</li>
</ul>

<ul id="sortable">
  <li class="ui-state-default">1</li>
  <li class="ui-state-default">2</li>
  <li class="ui-state-default">3</li>
</ul>

The script on the page is:

$(function() {
   $("#sortable").sortable();
   $("#sortable2 li").draggable({
      connectToSortable: "#sortable",
      helper: "clone"
   });
});

Additional CSS was added to style both UL elements as grids:

#sortable {
  list-style-type: none;
  margin: 0;
  padding: 0;
  width: 450px;
}
#sortable li {
  margin: 3px 3px 3px 0;
  padding: 1px;
  float: left;
  width: 100px;
  height: 90px;
  font-size: 4em;
  text-align: center;
}
#sortable2 {
  list-style-type: none;
  margin: 0;
  padding: 0;
  width: 450px;
}
#sortable2 li {
  margin: 3px 3px 3px 0;
  padding: 1px;
  float: left;
  width: 100px;
  height: 90px;
  font-size: 4em;
  text-align: center;
}

Seeking solutions or suggestions for this issue. Any ideas?

The libraries being used are jquery-ui.css, jquery-ui.js, and jquery.min.js.

Thank you!

Answer №1

The issue at hand is due to the fact that the children li elements have been floated.

When an element is floated, it is taken out of the normal document flow, causing the parent element to collapse with a height of 0:

https://i.sstatic.net/HF20u.png

Because of this, inserting elements into the #sortable element becomes impossible since it lacks a defined height.

To address this common problem, a popular solution involves using a clearfix to prevent the collapsing of the parent element.

You can either apply overflow: auto to the container element (example):

ul {
  overflow: auto;
}

or utilize a pseudo-element (example):

ul:after {
  content: '';
  clear: both;
  display: table;
}

Another approach would be to remove the float: left from the li elements and instead set their display to inline-block. Additionally, adding vertical-align: top can address minor alignment issues when dragging the elements (example):

ul li {
  display: inline-block;
  vertical-align: top;
}

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

"Create a collapsible side menu with Bootstrap and customize it with

My webpage layout is very basic, featuring a navbar at the top and a navigation menu down the left side of the page. <div class="container"> <nav class="navbar navbar-toggleable-md navbar-light bg-faded"> <button class="navbar- ...

Use jQuery to create a price filter that dynamically sets slide values based on the filter object

I'm currently utilizing the jQuery slide filter to sort products based on price. The filtering value is set within the script, but I am interested in dynamically setting it based on the highest/lowest number found on my page using a data attribute. D ...

Updating API calls with form submission in React.js

Currently working on a weather application and attempting to update my API call upon submitting a form. This project marks my initial attempt at developing my own program, and I've encountered an obstacle. The plan is for the user to input a city, cli ...

Utilizing the "apply" method with JavaScript OOP callbacks for dynamic context management

Encountering an issue while creating a callback in my custom EACH function utilizing Object-Oriented Programming principles. To achieve this, I have developed a JavaScript library that emulates the familiar habits of JQUERY. Experience it in action by ch ...

Creating beautiful prints with images

I have been tasked with developing an MVC C# application where the contents of a div are dynamically created using an AJAX call to fetch data from a database. Upon successful retrieval, the content is then displayed as a success message in JavaScript. Here ...

Display specific divs after selecting checkboxes and clicking the submit button

I need assistance with creating a page that contains multiple checkboxes for users to select. Once they click the submit button, specific divs should be displayed based on their checkbox selections. I am new to java coding and would greatly appreciate any ...

Identify the specific jQuery function that is triggered upon clicking a button

Hello everyone, I have a website using the theme from the following link: However, I am facing some issues with the functionality on my site and trying to troubleshoot the problem. For instance, when you click on the Breadcrumb link in the demo and see ho ...

Modify object rotation animation direction using keyboard controls in Three.js

Adjusting the object rotation direction with key controls is within my capability by utilizing the following code: case 37: scene.rotation.x -= 0.01; break case 38: scene.rotation.z -= 0.01 break Nevertheless, the rotation remai ...

Illuminate the rows in a table generated from a parsed text file using PHP

I am facing an issue with my PHP logic for selecting and highlighting rows in a table based on dropdown selection. I have a group of text files that are parsed into a table, and I need to highlight rows that match selections from 5 dropdowns created from t ...

Having trouble with the .load function not properly loading script functions. Is there a way to adjust the AJAX function

I attempted to use the .load function to load an HTML page with scripts, but unfortunately it is not working as expected. Is there a way to modify the existing code to utilize AJAX in order to load the complete HTML page along with its scripts? We specifi ...

Tips for concealing the scrollbar on Firefox while still allowing scrolling within a div container

Is there a way to hide the scrollbar in Mozilla Firefox without preventing scrolling within a specified div? I have tried using the following CSS, however, I do not want to include "overflow-x: hidden" in the div that already has a scrollbar. Additionally ...

Stop automated web crawlers from crawling through JavaScript links

To enable remote jQuery templating, I have embedded some links in JavaScript. For example: <script type="text/javascript"> var catalog = {}; catalog['key1'] = 'somepath/template_1.html'; catalog['key2'] = 'anotherp ...

The done() method in AJAX is triggered upon completion of all request processing tasks

After making an AJAX request to delete data from a list, I follow up with another AJAX request to update the list with new data. Here is the code for deleting data: $.ajax({ method: "DELETE", url: urlRequest, headers: { 'X-CSRF-TO ...

Using the scroll feature within the tooltip component in React with @material-ui/core/Tooltip can cause the entire page to

I am currently working with a tooltip created using the @material-ui/core/Tooltip library, and it is functioning correctly. You can see an example of the tooltip in action at this link: https://codesandbox.io/s/kgccc. https://i.stack.imgur.com/fMgGb.png ...

I am interested in tallying up the number of adjacent matching elements within an array and then providing the final count

I have an array and want to find all neighboring matches, count them, and then return the count. This needs to be done in a loop using .map() so that I don't need to store memory of what was counted beyond the current element. The number of current el ...

The issue with jQuery UI's resizable('destroy') function not functioning correctly when used in conjunction with the setTimeout method

Check out the jsFiddle demo here. Although the alert is triggered after 5 seconds, the destroy function does not seem to be working... Please share any suggestions or alternatives you may have. Thank you ...

Guide on extracting the text from the <script> tag using python

I'm attempting to extract the script element content from a generic website using Selenium. <script>...</script> . url = 'https://unminify.com/' browser.get(url) elements = browser.find_elements_by_xpath('/html/body/script[ ...

Arranging elements within distinct div containers

Utilizing Bootstrap 4, I crafted a card-deck containing two cards. Despite the equal height of both cards, the alignment of elements is affected by varying text lengths. <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min ...

How can I incorporate a custom icon into the <i> tag in HTML like Bootstrap does with its icons?

When working with Bootstrap, you can easily add an icon using the following syntax: <i class="bi bi-arrow-right-square-fill fs-1"></i> One great feature is that you can adjust the size of the icon by simply adding the fs-1 class. If ...

The navbar at the top of the page remains in place and obscures

I am facing an issue where my fixed navbar code breaks up into individual boxes for the links when I try to scroll, covering the page title. How can I resolve this? My code is too long to post directly here, so it's available on pastebin at the follow ...