Tips for eliminating a css property with jquery during a drag and drop operation

I have four pictures within a box, each with the individual IDs of box1, box2, box3, and box4.

Despite my efforts, when I attempt to drag and drop the pictures into boxright1, they do not land correctly inside the designated rectangle.

I attempted to use the disable property absolute on #box(number) in the dropped area to ensure proper placement within the rectangle... but unfortunately, this method was unsuccessful.

My objective is to successfully drop the images onto the rectangle as intended

What steps should I take to accomplish this?

function allowDrop(ev) {
  ev.preventDefault();
}
function drag(ev) {
  ev.dataTransfer.setData("text", ev.target.id);
}
function drop(ev) {
  ev.preventDefault();
  var data = ev.dataTransfer.getData("text");
  console.log(data);
   $("#data").removeAttr("position");
  ev.target.appendChild(document.getElementById(data));
}
#box1 {
  position: absolute;
  top: 25.3vh;
  left: -10.98vw;
  background-repeat: no-repeat;
  background-size: contain;
}

#box1 p {
  width: 10.0vw;
  height: 10.0vh;
  border-radius: 25%;
}


#box2 {
  position: absolute;
  top: 4.7vh;
  left: -10.98vw;
  
  cursor:pointer;
  border:px solid #0066CC;
  background-repeat:no-repeat;
  background-size: contain;
}

#box2 p {
 width: 5.0vw;
  height: 5.0vh;
  border-radius: 25%;
}

#box3 {
  position: absolute;
  top: 2.7vh;
  left: 43.98vw;
  background-size: contain;
  background-repeat:no-repeat;  
}

#box3 p {
  width: 7.0vw;
  height: 7.0vh;
  border-radius: 25%;
}

#box4 {
  position: absolute;
  top: 27.3vh;
  left: 40.98vw;
  background-repeat:no-repeat;
  background-size: contain;
}

#box4 p {
  width: 10.0vw;
  height: 10.0vh;
  border-radius: 25%;
}
.container2 {
  width: 50.1vw;
  position: fixed;
  top: 10.5vh;
  left: 30.5vw;
}

.boxright1 p {
  font-size: calc(2vw);
  height: 4vh;
  margin: 0;
  color: white;
}

.boxright1 {
  position: absolute;
  top: 65.3vh;
  left: 17.5vw;  
  width: 61.0vw;
  height: 35.0vh;  
  cursor:pointer;
  border:2px solid black;
  background-repeat:no-repeat;
  background-size: contain;
  background-image:url(images/name%20board%20witout%20rope2.png);
  background-size: 40vw 55vh; 
  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>not able to drop correctly on the rectangle:</p>
<div class="container2">  
    <div class="box" id="box1" draggable="true" ondragstart="drag(event)" style="background-image:url(https://picsum.photos/200/300)">
      <p name="values" id="name1" class="hidden"></p>
    </div>
        
    <div class="box" id="box2" draggable="true" ondragstart="drag(event)" style="background-image:url(https://picsum.photos/g/200/300)">
      <p name="values" id="name2" class="hidden"></p>
    </div>
        
    <div class="box" id="box3" draggable="true" ondragstart="drag(event)" style="background-image:url(https://picsum.photos/200/300?image=0)">
      <p name="values" id="name3" class="hidden"></p>
    </div>
        
    <div class="box" id="box4" draggable="true" ondragstart="drag(event)" style="background-image:url(https://picsum.photos/200/300/?gravity=east)">
      <p name="values" id="name4" class="hidden"></p>
    </div>   
</div>
<div class="boxright1" ondrop="drop(event)" ondragover="allowDrop(event)" id="4" name="d"></div>

https://i.sstatic.net/wM9j7.jpg

https://i.sstatic.net/AOJZe.jpg

Answer №1

To adjust the position, you can set it to unset. Also, make sure to use $("#"+data) since data is a variable.

$("#"+data).css("position","unset");

function enableDrop(event) {
  event.preventDefault();
}
function startDragging(ev) {
  ev.dataTransfer.setData("text", ev.target.id);
}
function endDrop(ev) {
  ev.preventDefault();
  var data = ev.dataTransfer.getData("text");
   $("#"+data).css("position","unset");
  ev.target.appendChild(document.getElementById(data));
}
#box1 {
  position: absolute;
  top: 25.3vh;
  left: -10.98vw;
  background-repeat: no-repeat;
  background-size: contain;
}

#box1 p {
  width: 10.0vw;
  height: 10.0vh;
  border-radius: 25%;
}


#box2 {
  position: absolute;
  top: 4.7vh;
  left: -10.98vw;
  
  cursor:pointer;
  border:px solid #0066CC;
  background-repeat:no-repeat;
  background-size: contain;
}

#box2 p {
 width: 5.0vw;
  height: 5.0vh;
  border-radius: 25%;
}

#box3 {
  position: absolute;
  top: 2.7vh;
  left: 43.98vw;
  background-size: contain;
  background-repeat:no-repeat;  
}

#box3 p {
  width: 7.0vw;
  height: 7.0vh;
  border-radius: 25%;
}

#box4 {
  position: absolute;
  top: 27.3vh;
  left: 40.98vw;
  background-repeat:no-repeat;
  background-size: contain;
}

#box4 p {
  width: 10.0vw;
  height: 10.0vh;
  border-radius: 25%;
}
.container2 {
  width: 50.1vw;
  position: fixed;
  top: 10.5vh;
  left: 30.5vw;
}

.boxright1 p {
  font-size: calc(2vw);
  height: 4vh;
  margin: 0;
  color: white;
}

.boxright1 {
  position: absolute;
  top: 65.3vh;
  left: 17.5vw;  
  width: 61.0vw;
  height: 35.0vh;  
  cursor:pointer;
  border:2px solid black;
  background-repeat:no-repeat;
  background-size: contain;
  background-image:url(images/name%20board%20witout%20rope2.png);
  background-size: 40vw 55vh; 
  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>unable to drop properly on the rectangle:</p>
<div class="container2">  
    <div class="box" id="box1" draggable="true" ondragstart="startDragging(event)" style="background-image:url(https://picsum.photos/200/300)">
      <p name="values" id="name1" class="hidden"></p>
    </div>
        
    <div class="box" id="box2" draggable="true" ondragstart="startDragging(event)" style="background-image:url(https://picsum.photos/g/200/300)">
      <p name="values" id="name2" class="hidden"></p>
    </div>
        
    <div class="box" id="box3" draggable="true" ondragstart="startDragging(event)" style="background-image:url(https://picsum.photos/200/300?image=0)">
      <p name="values" id="name3" class="hidden"></p>
    </div>
        
    <div class="box" id="box4" draggable="true" ondragstart="startDragging(event)" style="background-image:url(https://picsum.photos/200/300/?gravity=east)">
      <p name="values" id="name4" class="hidden"></p>
    </div>   
</div>
<div class="boxright1" ondrop="endDrop(event)" ondragover="enableDrop(event)" id="4" name="d"></div>

Answer №2

Take a look at this amazing library that features the necessary Sortable functionality. With this library, you can easily create and organize various types of DOM elements.

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 is the process for assigning a variable within a callback function?

Having trouble with assigning the dat.id received in a callback to my AccuprobeID variable, which is used for another API call later on. Any assistance would be greatly appreciated. I attempted changing sap.deviceInfo(callback) to sap.deviceInfo().then( ...

Removing a outside div element based on a dropdown change in ASP.NET jQuery

this is the structure of my unique div <div class="row-fluid"> <div class="span12"> <div style="overflow: auto; width: 90%;" class="goleft"> <div style="display: inline-block; width: 200px;"> ...

Need help aligning a column class perfectly in bootstrap?

I'm currently learning Bootstrap 3 and I have some HTML code that looks like this: <ul class="row> <li class="col-lg-3 col-md-3 col-sm-3 col-xs-6"></li> <li class="col-lg-3 col-md-3 col-sm-3 col-xs-6"></li> .. </ul ...

Ways to determine the count of selected checkboxes in React.js?

Recently, I delved into learning React. For one of my beginner projects, I decided to create a "life checklist" using Functional Components as the foundation. Just a heads up: I've structured data.js as an array of objects containing "action", "emoj ...

How to generate a JavaScript array by parsing JSON data received from an AJAX request

I am attempting to extract specific rows of data from a database so I can store them in a JavaScript array. The desired format of the array is outlined below for future use. var data = [ { column1: 'Test 1', column2: 'So ...

What is the method for adding a leading '+' sign to positive numbers using Intl.NumberFormat?

I'm currently utilizing Intl.NumberFormat in TypeScript/JavaScript within Angular2 to convert a numeric type into a formatted string. While this method is ideal, I am in need of a solution that would include a leading plus sign for positive numbers. ...

Implementing Pessimistic Locking in MongoDB for Collections

Hello, I am seeking your expertise and creative guidance on a project I have been struggling to solve. My goal is to enable users to manipulate a collection of orders in a stock market application. It is imperative that these orders are executed one by one ...

Transmit targeted information to a different webpage

Hello, I have multiple radio buttons but I only want to send selected ones to my insert PHP file. How can I choose specific inputs and send them to another page with just one button? My code is quite lengthy. This looks like a duplication x7 Welcome to ...

Sending postMessage during the beforeunload event does not work as expected

When postMessage() is triggered within the beforeunload window event in an Ionic 2 browser, I've noticed that the message doesn't make it to the parent. However, if the same message is sent during the unload or load event, it is received successf ...

Could someone break down for me the behavior exhibited within this method?

Hello there, I'm a beginner so please forgive me for any lack of knowledge. const example = { myFunction(){ console.log(this); }, myFunction2(){ function myFunction3(){ console.log(this) } return ...

Leverage Typescript to convert string literal types to uppercase

type x = 'first' | 'second' I am looking to create a type y that is similar to this: type y = 'FIRST' | 'SECOND' Here is what I attempted: type x = 'first' | 'second' type y = {[key in x]: key[& ...

Refresh the block's content seamlessly without the need for a page reload

Within the index.html page There exists a block called content that contains various content blocks. An additional navigation menu with numerous links is present. It is important that when you click on a menu link, the content within the content block re ...

Delays in running multiple jQuery UI effects at the same time

When I implement a show and hide effect with slide on different divs simultaneously on my page, I encounter some lag in the animation. However, I noticed that if I run the show effect only after the hide effect is completed, the lag disappears. I am curiou ...

Is it more effective to import an entire library or specific component when incorporating it into Create-React-App?

I have a question about optimizing performance. I understand that every library has its own export method, but for instance, on react-bootstrap's official documentation, it suggests: It is recommended to import individual components like: react-boo ...

In HTML, specify that the height of both the `html` and

I am facing an issue with the container-about div. I have set its height to 100% so that the div occupies the entire width and height after the header div. The problem is that currently, I cannot scroll to view the full text. It would be great if there wa ...

Guide to deactivating validation in Plumier

I'm currently facing a challenge that requires me to perform manual validation within the controller. I've searched through the documentation but couldn't find any instructions on how to disable automatic validation. Is there a workaround av ...

Should I be decoding strings from the server like this in my JavaScript code?

When retrieving JSON data from an ASP.NET web service that has been HtmlEncoded using Microsoft's AntiXSS library (Encoder.HtmlEncode()), and returned as JSON through a jQuery Ajax call, I am faced with the task of setting this data on form inputs. O ...

Invoke the ngrx component store's Effect in a synchronous manner

In the ComponentStore of ngrx, we have two effects. readonly startAndUpdateProgress = this.effect<void>( (trigger$) => trigger$.pipe( exhaustMap(() => this.numbersObservable.pipe( tapResponse({ next: (p ...

Guide to executing API PATCH request on the server for a system that approves outings?

I have developed a system for approving outings using the MERN Stack. I wrote code to change the leave status to "Approved", but the changes are not saved when I refresh the page. The PATCH request works fine through Postman. Mongoose Schema Snippet for l ...

How to send an AJAX request using jQuery?

I am struggling to locate specific examples of what I need. My objective is to access sample.aspx and send plain text parameters using the POST method (not as part of the query string). Upon successful submission, I aim to analyze the JSON response. In cas ...