Rearrange and place items at the dropped location

I am looking to create a drag-and-drop feature for moving items from a list of products to an empty container. Upon completion, I need to save the location and the selected items in a database so that I can recall this layout later.

However, I have encountered a hurdle early on in the process. I have been able to successfully drag and drop items from the list to the container, but they are not displaying at the coordinates I specified; instead, they appear in a tabular format.

  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ShoppingCart.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
    <script src="scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        var srcElement;

        $(document).ready(function () {

            $("div .product").each(function () {
                this.addEventListener('dragstart', OnDragStart, false);
            });

            $("div .bag").each(function () {
                this.addEventListener('dragenter', OnDragEnter, false);
                this.addEventListener('dragleave', OnDragLeave, false);
                this.addEventListener('dragover', OnDragOver, false);
                this.addEventListener('drop', OnDrop, false);
                this.addEventListener('dragend', OnDragEnd, false);
            });

        })

        function OnDragStart(event) {
            srcElement = this;

        }

        function OnDragOver(e) {
            e.preventDefault();


            return false;
        }

        function OnDragEnter(e) {
            e.preventDefault();
        }

        function OnDragLeave(e) {
            e.preventDefault();
        }

        function OnDrop(e) {
            e.preventDefault();

            var dm = srcElement.cloneNode(true);

            dm.style.Left = e.offsetX;
            dm.style.Top = e.offsetY;
            dm.style.position = "absolute";

            //$(this).append(srcElement);
            e.target.appendChild(dm); 

            e.stopPropagation();
            return false;
        }

        function OnDragEnd(e) {
            e.preventDefault();
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table >
                <tr>
                    <td>
                        <div draggable="true" id="div1" class="product">
                            <header>Rieker Trekkingsandalen braun</header>
                            <img src="images/monitor.jpg" style="height: 150px; width: 150px;" />
                        </div>
                    </td>
                    <td>
                        <div id="div2" class="product" draggable="true">
                            <header>Rieker Trekkingsandalen braun</header>
                            <img src="images/Cart.jpg" style="height: 150px; width: 150px;" />
                        </div>
                    </td>
                    <td>
                        <div id="div3" class="product" draggable="true">
                            <header>Rieker Trekkingsandalen braun</header>
                            <img src="images/mouse.jpg" style="height: 150px; width: 150px;" />
                        </div>
                    </td>
                    <td>
                        <div id="div4" class="product" draggable="true">
                            <header>Rieker Trekkingsandalen braun</header>
                            <img src="images/speaker.jpg" style="height: 150px; width: 150px;" />
                        </div>
                    </td>
                </tr>
            </table>

            <div class="bag">
            </div>
        </div>
    </form>
</body>
</html>

.product {
    height: 300px;
    width: 150px;
    border: 2px solid #666666;
    background-color: white;
    text-align: center;
    cursor: move;
}
.bag {
    background-color: green;
    height: 500px;
    width: 500px;
    position: absolute;
}

Can someone assist me with identifying the issue here?

Cheers, Stefan

Answer №1

dm.style.position = "fixed";

needs to be

dm.style.position = "absolute";

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

The sinuous waveform in JavaScript

track : function(x, y, top, ampl) { return { top : top + 2, x : x + ampl * Math.sin(top / 20), y : (top / this.screenHeight < 0.65) ? y + 2 : 1 + y + ampl * Math.cos(top / 25) }; } This specif ...

The clash between Kendo UI and jQuery UI

I implemented Kendo UI for the date picker, and I'm looking to incorporate jQuery UI for the autocomplete feature. Upon adding the jQuery auto complete to my code, I encountered the following error: Uncaught TypeError: Cannot read property 'e ...

Exploring the functionality of CodePen's code editor in relation to developing a 2D shooting game

Recently, I created a straightforward 2D shooter game with all the code neatly organized in a single HTML file: (file_gist). When I tested the game in my chrome browser, everything worked flawlessly according to my intentions. However, upon transferring th ...

Is there a way to remove the excess white space beneath my content without resorting to using overflow-y: hidden?

I am struggling to eliminate the excess white space below the body of my web page in order to make it fit perfectly within the view of a web browser. Adding height helps with vertical alignment but leaves unwanted white space. I need a solution that doesn ...

Exploring the capabilities of ECMAScript generators within the Intel XDK Simulator

I am attempting to utilize a generator that has been declared using function* in Intel XDK. The simulate function within XDK is said to be based on Chromium, though it's difficult to determine the specific version ('about' box and similar fe ...

Using environmental variables in Nuxt 2 or Nuxt 3 - a step-by-step guide

I have an .env file located in the root of my project. In my nuxt config, I am using variables to configure ReCaptcha as shown below: import dotenv from 'dotenv' dotenv.config() export default { modules: [ ['@nuxtjs/recaptcha&ap ...

Strategies for detecting changes in multiple HTML select elements with jQuery

I currently have three select HTML tags and I would like to filter my table using the selected values (group of selected tags) with an AJAX request. For example, filtering by Gender, Location, and Season. My question is how can I achieve this without dup ...

Parse a string in the format of "1-10" to extract the numbers and generate an array containing the sequence of numbers within the range

Looking to convert a string in the format "1-10" into an array containing the numbers within that range. Display the array on the screen using a for loop. For example, if "1-5" is provided, the resulting array should be: {1, 2, 3, 4, 5} Create a workflow ...

Is there a way to execute this process twice without duplicating the code and manually adjusting the variables?

I'm looking to modify this code so that it can be used for multiple calendars simultaneously. For example, I want something similar to the following: Here is what I currently have: This is my JavaScript code: var Calendar = { start: function () ...

Utilize jQuery to extract information from a webpage and retrieve a JSON object

Looking to extract specific JSON data from a webpage on the same domain that is nested inside a <script> tag with a unique id. <div id="someID"> <script type="text/json" id="scriptID"> { "some more here": "and there", ...

Is it possible to embed a stringified JSON object into the query string or URL using asp.net-mvc?

Recently, I've been working with a controller action on my asp.net-mvc site where I have the following code: asp.net-mvc Controller Action: public ActionResult MyAction(PostParams myParams) { //perform tasks } The PostParams class is defined as ...

What is the best way to choose all elements except for <body>? Alternatively, how can <body> be styled to its default appearance?

Web browsers often apply default styles to different elements, but users have the option to override these defaults using custom stylesheets. I personally like to customize these default styles with my own, for example: * {padding:0; margin:0; } However ...

Guide on sending information using ajax using a button within a form

I've been working on creating a form that utilizes HTML5 validations and integrates Ajax to show the status of mail sending. However, I'm facing an issue where clicking the send button does not initiate the expected action. Form HTML: <div c ...

The Firebase storage percentChanges() method is throwing a NaN error

I want to create a Firebase storage service using an Angular client to handle user profile image uploads. This service should return two observables: uploadProgress$ and downloadUrl$. The uploadProgress$ observable will store the value from percentChanges ...

Steps to make a sub Post or page on WordPress

Seeking to add sub posts within my website's main posts. The URL structure for the main post will be: site.com/post-title I envision sub posts following this format: site.com/post-title/subpost1 site.com/post-title/subpost2 site.com/post-title/ ...

Unable to modify the color of UML state elements within JointJS

I need some help with jointjs as I am in the process of adjusting the color of a UML state diagram graph using this command: graph.getElements()[4].attributes.attrs[".uml-state-body"]["fill"] = "#ff0000"; However, despite trying this, the color of the st ...

Using a function with a parameter as an argument in an event handler

Imagine you have the code snippet below: $('#from').focus(listExpand(1)); $('#to').focus(listExpand(3)); I am facing an issue as the code is not behaving as expected. I believe the problem lies in passing a function result instead of ...

Saving asp.net strings in different formats to datetime fields in a database

Can someone please assist me with saving the string value of textbox to the database as a datetime, and for updating/editing purposes, display it again in the textbox as a string? The reason for this is that the datetime input will be copied from Outlook a ...

Building a nested table within a parent table in bootstrap is a breeze

Can you provide guidance on nesting tables in Bootstrap-3? Whenever I attempt it, the second table ends up being displayed after the first one. ...

Error: Attempting to assign value to the 'innerHTML' property of null in a Wordle clone with local storage

While developing a Wordle-inspired game for my website, I decided to utilize Local Storage to store the user's progress. Everything seemed to be working smoothly until an unexpected error occurred: "Uncaught TypeError: Cannot set properties of null (s ...