Title menu that can be both dragged and edited

I am currently working on developing an HTML user interface that enables my users to rearrange or organize the menu items or utilities according to their preference, much like the HOME screens on Android or iOS devices. I am seeking advice on how I can save the positions of the menus?

$(function() {
  $("#items").sortable({
    placeholder: "highlight",
    start: function(event, ui) {
      ui.item.toggleClass("highlight");
    },
    stop: function(event, ui) {
      ui.item.toggleClass("highlight");
    }
  });
  $("#items").disableSelection();
});
.list {
  background-color: #478BFF;
  font-size: 30px;
  text-align: center;
  cursor: pointer;
  font-family: Geneva, Arial, Helvetica, sans-serif;
  border: 1px solid gray;
}

#items .ui-selected {
  background: red;
  color: white;
  font-weight: bold;
}

#items {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

#items li {
  float: left;
  margin: 2px;
  padding: 2px;
  width: 48px;
  height: 48px;
  line-height: 48px;
}

.highlight {
  border: 1px solid red;
  font-weight: bold;
  font-size: 45px;
  background-color: lightblue;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<div style="width:520px">
  <ul id="items">
    <li class="list">1</li>
    <li class="list">2</li>
    <li class="list">3</li>
    <li class="list">4</li>
    <li class="list">5</li>
    <li class="list">6</li>
    <li class="list">7</li>
    <li class="list">8</li>
    <li class="list">9</li>
    <li class="list">10</li>
    <li class="list">11</li>
    <li class="list">12</li>
    <li class="list">13</li>
    <li class="list">14</li>
    <li class="list">15</li>
    <li class="list">16</li>
    <li class="list">17</li>
    <li class="list">18</li>
    <li class="list">19</li>
    <li class="list">20</li>
    <li class="list">21</li>
    <li class="list">22</li>
    <li class="list">23</li>
    <li class="list">24</li>
    <li class="list">25</li>
    <li class="list">26</li>
    <li class="list">27</li>
    <li class="list">28</li>
  </ul>
</div>
<div id="info" style="font-family:Geneva,Arial,Helvetica,sans-serif;font-size:20px;"></div>

Answer №1

Utilizing a straightforward .map() function will retrieve the current positions of each item:

function fetch() {
  let positions = $.map(
    $('#items li'), 
    function(i){
      return parseInt($(i).text())
    });
  console.log(positions);
}

fetch();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div style="width:520px">
  <ul id="items">
      <li class="list">1</li>
      <li class="list">2</li>
      <li class="list">3</li>
      <li class="list">4</li>
      <li class="list">5</li>
      <li class="list">6</li>
      <li class="list">7</li>
      <li class="list">8</li>
      <li class="list">9</li>
      <li class="list">10</li>
      <li class="list">11</li>
      <li class="list">12</li>
      <li class="list">13</li>
      <li class="list">14</li>
      <li class="list">15</li>
      <li class="list">16</li>
      <li class="list">17</li>
      <li class="list">18</li>
      <li class="list">19</li>
      <li class="list">20</li>
      <li class="list">21</li>
      <li class="list">22</li>
      <li class="list">23</li>
      <li class="list">24</li>
      <li class="list">25</li>
      <li class="list">26</li>
      <li class="list">27</li>
      <li class="list">28</li>
  </ul>
</div>

In order to execute this code upon button click instead of page load, and to prevent naming conflicts, consider binding it accordingly and providing a distinct name.


Note: When needing to assign a common class to all children within a parent element, removing individual classes from items and styling them via the parent is often a better approach. For instance:

<ul id="items">
  <li>1</li>
  <li>2</li>
  ...
</ul>

You can apply styles to the <li>s with #items>li{ ... } rather than using #items .list{ ... }

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

Having trouble transferring a PHP string to jQuery

I recently set up a script on my computer that displays a list of active interfaces: $ interfaces eth0 lo wlan0 Now, let me share my PHP code snippet with you: <?php $output=shell_exec('./interfaces'); $string = trim(preg_replace(&ap ...

The issue with the Timber Ajax-cart drawer is that it fails to update the quantity of added or removed products

Check out my site: with the password set to "satin" I've integrated the Timber ajax-cart feature into my theme, but I'm facing an issue where the cart quantity doesn't update when clicking the "+" or "-" buttons in the drawer. Any help wo ...

Troubleshooting issues with drawing images on HTML5 canvas: experiencing unexpected outcomes

As I am diving into learning how to use the HTML5 canvas element, I have had success in drawing rectangles, utilizing getImageData, manipulating pixels, and then using putImageData to witness the color changes of the rectangles, among other capabilities. ...

Guide to finding the parent component of a particular element?

When the add button is clicked, I am dynamically adding lists and a new button labeled as "New Button" to a web page. My goal is to have a variable printed inside the parent list when this "New Button" is clicked. How can I achieve this functionality? Bel ...

Placing text and an image within a grid cell does not automatically stack them on top of each other

I have a large div with a grid display, consisting of 3 rows and 2 columns. I am attempting to include text directly below the photo within each cell. This is what my code looks like: <div class="arrange-fixes"> ...

Is the rotate() method in SVG supported on web browsers when passing in additional parameters like angle, centerX, and centerY similar to the CSS rotate

I have recently learned about a rotate transform that allows for rotating around a specified center point using the `rotate(angle, centerX, centerY)` method. However, I have noticed that this method does not seem to work when applied via CSS. Interestingl ...

A crisscrossing dashed design running along the edges of the text block

<div class="search"> <p>SEARCH</p> </div> https://i.sstatic.net/yxOga.png I am attempting to incorporate a dashed lateral padding similar to the one shown in the image, but only on the sides of the text. Is there a method I can ...

The jQuery AJAX function executing twice upon click

I've encountered an issue while attempting to make two Ajax calls on a single page using jQuery. The first Ajax call executes successfully and generates the desired results. However, the second Ajax call is meant to be triggered by clicking a button b ...

Use a PHP form to send an email with HTML formatting, rather than plain text

I am facing an issue with sending emails from a web form. The received email needs to be displayed as HTML and not text. function createMailBodyLine ( $title, $value ) { $value = nl2br(htmlspecialchars($value)); return "<tr> ...

Is there a way to modify the background color with Bootstrap?

While following a Bootstrap tutorial, I stumbled upon an issue that I can't seem to resolve. In my layout, I have an article and a sidebar in a row, with the article taking up 9 columns and the sidebar taking up 3 columns. The background color of the ...

Having trouble with Angular UI Select functionality?

I have integrated the angular ui select library from https://github.com/angular-ui/ui-select into my project. Instead of using the traditional select element, I am now utilizing the ui-select directive. This is a snippet of my code: <select class=" ...

How to conceal table cells on Safari 5?

My code works on Firefox and IE, but not on Safari. Here's an example: <table> <thead> <tr> <th style="display: none;">hi</th> </tr> </thead> <tr class="someClass"> <td style= ...

Exploring the combination of PHP and HTML through conceptual sessions

I am a beginner in the world of PHP, HTML, and website development, and I am currently trying to grasp the concept of Sessions. My goal is to implement a login/logout feature on a website using sessions, MySQL, and Slim framework. I am struggling with und ...

Use PHP to create a new JSON file on the server by submitting a form, then utilize a second form to update

My current goal is to create a json file using a form and then update that file with another form. Both of these processes are handled in the process.php file. I have managed to successfully update the json file if it is named as data.json initially in pro ...

The Ajax request fails to trigger the success function following the JsonResult response

Attempting to utilize Ajax to add a record to the database and retrieve data from JsonResult upon successful insertion leads to an error message: parseerror. Oddly enough, the record is successfully added to the DB. Below is the code for the post method: ...

I attempted various methods but was unable to successfully call the webmethod in the code behind using jQuery Ajax and Knockout

I have attempted various solutions, such as enabling friendly URL resolution and utilizing web method with session enabled, but unfortunately, the issue remains unresolved. I kindly request your assistance in resolving this matter. Here is my HTML code for ...

When I scroll and update my webpage, the navigation header tends to lose its distinct features. This issue can be resolved

When I scroll, my navigation header changes from being transparent to having a solid color, and this feature works perfectly. However, whenever I refresh the page while already halfway down, the properties of my navigation header are reset and it reverts ...

When clicking on an HTML link pointing to a cloud, it does not open in the same frame but instead opens in a new window

I am currently working on an HTML5 application that features a screen divided into two parts. The left part of the screen contains a few links, and upon clicking one of these links, the resulting content should open in the right side frame within the same ...

Accessing a variable outside of the function call in AngularJS is not possible

After starting to tackle AngularJS, I've encountered an issue that's been plaguing me. It seems like I'm unable to access the data returned by $http.get() outside of the method call. Here's a look at the code snippet: (function(){ ...

Is there a way to display a div element just once in AngularJS?

I only want to print the div once and prevent it from printing again. $scope.printDiv = function(divName) { var printContents = document.getElementById(divName).innerHTML; var popupWin = window.open('', '_blank', 'width=300, ...