Ensure a consistent number of draggable elements in HTML/JavaScript

Below is the HTML/JS code snippet:

<!DOCTYPE HTML>
<html>
<head>
<script>

        function allowDrop(ev) {
            ev.preventDefault();
        }

        function drag(ev) {
            ev.dataTransfer.setData("text", ev.target.id);
            this.style.opacity = '0.4';
        }

        function drop(ev) {
            ev.preventDefault();
            var data = ev.dataTransfer.getData("text");
            ev.target.appendChild(document.getElementById(data));
        }

        </script>
</head>
<body>
    <!--Create Left Panel-->
    <div id="leftPanel" class="container-left">
        <!--Create subpanel for each data (submenus) and add initial divs (elements)-->
        <div id="panelEntrada" class="container-left-entrada" ondrop="drop(event)" ondragover="allowDrop(event)">
            <div id="draggable1" class="capaEntrada" draggable="true" ondragstart="drag(event)"></div>
        </div>

        <div id="panelOculta" class="container-left-oculta" ondrop="drop(event)" ondragover="allowDrop(event)">
            <div id="draggable2" class="capaOculta" draggable="true" ondragstart="drag(event)"></div>
        </div>

        <div id="panelSalida" class="container-left-salida" ondrop="drop(event)" ondragover="allowDrop(event)">
            <div id="draggable3" class="capaSalida" draggable="true" ondragstart="drag(event)"></div>
        </div>

        <!--Divide in 2 sections for each type of activation function-->
        <div id="panelActivacion" class="container-left-activation">
            <div id="panelSigmoidal" class="container-left-activation-left" ondrop="drop(event)" ondragover="allowDrop(event)"> 
                <div id="draggable4" class="funcSigmoidal" draggable="true" ondragstart="drag(event)"></div>
            </div>

            <div id="panelTanh" class="container-left-activation-right" ondrop="drop(event)" ondragover="allowDrop(event)">
                <div id="draggable5" class="funcTanh" draggable="true" ondragstart="drag(event)"></div>
            </div>
        </div>
    </div>
    <!--container right is all the right area(top, right and area de design)-->
    <div class="container-right">
            <!--top just the top part-->
            <div class="container-top"></div>
            <!-- all that's left of the middle part (design y right)-->
            <div class="container-bottom">
                <div id="bottom" class="container-bottom-left" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
                <div class="container-bottom-right"></div>
            </div>
        </div>

</body>
</html>

This combined with another CSS code creates the resulting window shown in the image below: https://i.sstatic.net/9tSSw.png

The small unfilled rectangles on the left side are draggable within all the other rectangles in the left panel (green, black, gray, aqua, and magenta) and can also be moved to the larger green rectangle in the middle. The goal is to keep a fixed number (just one) of each rectangle in its original position while allowing new ones to be added to the design area like a drawer.

Do you have any suggestions on achieving this functionality using only HTML, JS, and CSS?

Answer №1

Discovered the resolution by simply modifying the line below

ev.target.appendChild(document.getElementById(data));

to

ev.target.appendChild(document.getElementById(data).cloneNode(true));

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

Using jQuery's deferred.done method to send an array of data to the done function

I followed the method outlined in https://api.jquery.com/jquery.when/ to execute a series of ajax calls (using the example $.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) ) on that page). The code below is functional; however, I am struggling with ...

Issue with Sidebar Menu Title Display in AdminLte Interface

The AdminLTE theme sidebar menu title does not display properly when the title is long. Instead, it breaks the line and shows the text below the icon. I would like it to start below the first letter of the first line. I have attached a screenshot for refer ...

The function Document.Open() is not functioning correctly

As I work on my webpage, I am having an issue with a button. This button should notify the user if their username and password are correct, and then redirect them to another page. However, while the notification works fine, the redirection does not happen ...

Adding a border in jQuery or Javascript to highlight empty form fields

After making the decision to dive into the world of Javascript, I've been dedicated to perfecting a script that will encase empty elements with a border right before the user submits a form. My ultimate goal is to implement live validation in the form ...

What methods can be used to exclusively force XMLHttpRequest to utilize the ISO-8859-1 charset?

My database uses the ISO-8859-1 codepage, and I prefer to make all AJAX requests in this codepage. Can you provide guidance on how to correctly set the content-type for AJAX requests? ...

The function is failing to provide a return value

In the Game.js file, there is a function that I am working on Game.prototype.onClick = function(event){ var x = event.x; var y = event.y; if (this.blueDeck[0].contains(x, y)) alert("Blue Deck Clicked"); } The OnClick function is ca ...

React: The `style` attribute requires a key-value mapping of style properties, rather than a simple string

I am attempting to use React to render the following HTML code: <a style='background-color:black;color:white;text-decoration:none;padding:4px 6px;font-family:-apple-system, BlinkMacSystemFont, "San Francisco", "Helvetica Neue", Helve ...

Steps for Sending Data Using a URL

I am having trouble deleting a row based on its id. Below is the code I am using: <tr> <th>ID</th> <th>Nom etudiant</th> <th>Prenom etudiant</th> ...

Using jQuery to loop through a table and retrieve the button value from every row

I have a challenge with dynamically created buttons in a table. My goal is to loop through the table, identify the rows that have a checked checkbox, and retrieve the value of a button within that row. I then need to store these values in an array. Unfortu ...

Troubleshooting: Css navbar alignment

How can I center the navigation bar both horizontally and vertically? I've tried various methods but nothing seems to work. Here is my HTML: <section class="navigation" ng-controller="NavController"> <div class="nav-container"> ...

Surprising behavior encountered while utilizing fsPromises.open with Node.js

As I work on a larger app, I encounter an issue with a file writing operation. I am utilizing fsPromises to generate an autosave file, but the path variable seems to lose its value between a console log for debugging and the actual call to open the file. I ...

The navigation in Framework 7 is causing issues with the on-click functionality

Utilizing the framework's built-in formToJSON() function, I have been able to retrieve form values. By utilizing a click event, I am able to log the values. $$("#query-submit").on("click", function () { var queryForm = app.formToJSON("#query-form ...

Uploading Files with PhoneGap using Ajax

I have been attempting to execute this PhoneGap example for uploading images from a device to a server. // Wait for PhoneGap to load // document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap is ready // functi ...

changing web pages into PDF format

I need to convert HTML to PDF on a Linux system and integrate this functionality into a web application. Can you suggest any tools that are capable of doing this? Are there any other tools I should consider for this task? So far, I have attempted the foll ...

Restrict date range in Bootstrap Datepicker based on database values

Is there a way to remove specific date ranges from the database? For instance, if I have date ranges from 15.01.2021 to 21.01.2021 and from 24.01.2021 to 03.02.2021, is it possible to prevent these dates from being selected in the datepicker? I would lik ...

Creating an overlay with CSS hover on a separate element and the ::before pseudo class

Issue: I am struggling to display the overlay when hovering over the circle element, rather than just the image itself. I have attempted to achieve this using CSS, but can't seem to make it work as intended. Any guidance and examples using JavaScript ...

Struggling with loading external scripts within background.js in Chrome extensions?

I am facing an issue with my chrome extension. I am unable to call an external script, specifically the ethereum script (web3.min.js), from within my background.js file. The error message I receive is: Uncaught EvalError: Refused to evaluate a string ...

Develop a custom class for importing pipes in Angular 4

Currently, I am working on creating a pipe that will replace specific keywords with the correct strings. To keep this pipe well-structured, I have decided to store my keywords and strings in another file. Below is the code snippet for reference: import { ...

Internet Explorer: JQuery deselects radio button upon its selection

One issue I have encountered is with a listener for a group of radio buttons in IE. When a radio button is selected, it triggers a database call to populate a select element. However, in IE, after the code runs successfully, the selected radio button becom ...

Issue with Node.js - Jade form not submitting or functioning properly?

app.js /** * Module dependencies. */ var express = require('express'); var routes = require('./routes'); var http = require('http'); var path = require('path'); var app = express(); // all environments app.s ...