Is it possible to target a paragraph element nested within a class element using the ondrop event and then remove it?

I have two div boxes: .box001 and .box002.

I need to remove the p elements with names name1, name2, and name3 on each drop action.

When I drag and drop a box002 element with id="1" to another div - box001 with id="10", the dragged element should be deleted upon dropping and the background-color should become none. The same should happen when dragging box002 with id="2" to box001 with id="20", as well as for the third box accordingly.

What is the best way to achieve this functionality?

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");
  alert(data);
  var el = document.getElementById(data);

  el.parentNode.removeChild(el);    // removing the dragged item

  ev.target.style.backgroundColor = 'initial'; //[value indicate which box elemenet] bgcoclor none
  document.getElementsByClassName(ev.target.className).innerHTML = '';  // clear the content of the specific box
}
.box001 {
  float: left;
  width: 50px;
  height: 50px;
  border: 1px solid black;
  border-radius: 10%;
  background-color: #42e0fd;
  font: "Courier New", Courier, monospace;
  font: 70px;
  ;
  color: #001a00;
  font-size: xx-small;
  font-weight: 900;
  text-align: center;
}

.box002 {
  float: left;
  width: 50px;
  height: 50px;
}
<div class="box001" ondrop="drop(event)" ondragover="allowDrop(event)" id="10">
  <p id="11"> name1</p>
</div>
<div class="box001" ondrop="drop(event)" ondragover="allowDrop(event)" id="20">
  <p id="12">name2</p>
</div>
<div class="box001" ondrop="drop(event)" ondragover="allowDrop(event)" id="30">
  <p id="13">name3</p>
</div>


<div class="box002" draggable="true" ondragstart="drag(event)" id="1">
  <img src="https://picsum.photos/200/300" draggable="true" id="1" style="width:50px; height:50px; border-radius: 50%;" border="rounded" />
</div>
<div class="box002" draggable="true" ondragstart="drag(event)" id="2">
  <img src="https://picsum.photos/200/300" draggable="true" id="2" style="width:50px; height:50px; border-radius: 50%;" border="rounded" />
</div>
<div class="box002" draggable="true" ondragstart="drag(event)" id="2">
  <img src="https://picsum.photos/200/300" draggable="true" id="3" style="width:50px; height:50px; border-radius: 50%;" border="rounded" />
</div>

Answer №1

The new addition to the Javascript container can be found at the bottom for easy reference.

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");
  alert(data);
  var el = document.getElementById(data);

  el.parentNode.removeChild(el); // This line deletes the dragged item

  ev.target.style.backgroundColor = 'initial'; // Resetting the background color of the box
  
  /* New Functionality */
  var pParagraph = ev.target.firstElementChild;
  console.log(pParagraph);
  ev.target.removeChild(pParagraph);
}
.box001 {
  float: left;
  width: 50px;
  height: 50px;
  border: 1px solid black;
  border-radius: 10%;
  background-color: #42e0fd;
  font: "Courier New", Courier, monospace;
  font-size: xx-small;
  font-weight: 900;
  text-align: center;
}

.box002 {
  float: left;
  width: 50px;
  height: 50px;
}
<div class="box001" ondrop="drop(event)" ondragover="allowDrop(event)" id="10">
  <p id="11"> name1</p>
</div>
<div class="box001" ondrop="drop(event)" ondragover="allowDrop(event)" id="20">
  <p id="12">name1</p>
</div>
<div class="box001" ondrop="drop(event)" ondragover="allowDrop(event)" id="30">
  <p id="13">name1</p>
</div>


<div class="box002" draggable="true" ondragstart="drag(event)" id="1">
  <img src="https://picsum.photos/200/300" draggable="true" id="1" style="width:50px; height:50px; border-radius: 50%;" border="rounded" />
</div>
<div class="box002" draggable="true" ondragstart="drag(event)" id="2">
  <img src="https://picsum.photos/200/300" draggable="true" id="2" style="width:50px; height:50px; border-radius: 50%;" border="rounded" />
</div>
<div class="box002" draggable="true" ondragstart="drag(event)" id="2">
  <img src="https://picsum.photos/200/300" draggable="true" id="3" style="width:50px; height:50px; border-radius: 50%;" border="rounded" />
</div>

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

Managing a conditional operator in JavaScript and React - what you need to know!

I am working with the following JavaScript code: const { inverseDirection } = this.props; If the value of inverseDirection is false, I want to execute the following: const dashOffset = dashArray - (dashArray * this.state.percentProgress) / 100; But i ...

What is the best way to create a fixed sidebar in Bootstrap?

Is there a way to create a sticky sidebar similar to cpmta.org using Bootstrap? I'm struggling to figure it out... ...

PHP and HTML form: The ultimate guide to sending emails through your website

Currently, I am in the process of learning PHP and attempting to send an email to myself using an HTML form. However, I am experiencing some difficulties. <form action="index.php" role="form" method="post" name="emailform"> ...

The struts2-jquery tags do not support dynamic input recognition

In my project, there is a requirement to dynamically update the content of the <s:textfield>. Here is the code snippet I have implemented: <script type="text/javascript"> $.subscribe('before', function(event, data) { ...

Retrieve the response from a $.ajax request in PHP

I am trying to retrieve data using an Ajax call Below is the code to fetch the ID <h3> <a class='click' data-id='<?=$rows["id"];?>'><?=$rows['title'];?></a</h3> This is my jQuery code ...

IE is experiencing issues with the multiple select option not functioning properly

I am encountering an issue with a multiple select dropdown. When I choose "Tous" (All), I want all options to be selected except the first one. This functionality works perfectly on Chrome, Firefox, and Safari, but for some reason, it doesn't work on ...

Modify the dropdown menu title dynamically based on the selection made in Angular

My Angular web-application has a dropdown menu that looks like this: <div class="btn-group" dropdown> <button dropdownToggle type="button" class="btn btn-primary dropdown-toggle">NAMEOFDROPDOWN <span class="caret"></span>&l ...

Effective strategies for managing form submissions with React and Typescript

As I dive into refactoring my code to TypeScript, especially as I am still getting accustomed to it, I find myself pondering about the HTML element types with React events. This has led me to rethink how I approach form creation and submission event handli ...

Secure login using AngularJS

I am in need of modifying the code on this Plunker: plnkr.co/edit/Mvrte4?p=preview I want to remove user roles so that all users have access to the same page. If possible, I would like the modified code to be split into two pages: Page 1: index.html co ...

Advantages of Using JSON for POSTing Data to a Server with jQuery

While browsing the internet, I've come across various examples of using jQuery to send AJAX POST requests with JSON encoded data to a server. I'm curious about why it's necessary to encode the data in JSON first. Wouldn't it be more eff ...

Jquery enhance the speed of appending elements

I have written a short script and I am looking to achieve a slow fade-in effect for the "appearance" instead of it just popping in. How can I do that? I want to maintain the append functionality as well. <script> $( "#text" ).hover( functio ...

The promise node design pattern

I'm dealing with a node issue where I need to call a Data Access Object and possibly others within it, and then render a Jade template once everything is done. Here's an example of what I want to achieve: provider1.getData(args, function(error ...

Change jquery datatable information into json format

I am currently utilizing a jQuery DataTable with the following table structure, <table id="employees"> <thead> <tr> <th>Id</th> <th>Name</th> <th>Email</th> <t ...

Refreshing modal content following successful AJAX call

Does anyone know how to fix this issue in my code? I need to reload a modal for another database query after a successful operation if(result === 'success'){ $("#editarModalOcup").modal('hide'); $('#success .modal-body'). ...

When the checkbox is selected, I would like Bvalidator to validate specific fields

Currently, I am dealing with a form that utilizes bValidator for validation. The problem arose when I needed the validation to only apply to specific fields when a checkbox is checked, and to all fields when the checkbox is not selected. The challenge lies ...

Tips for changing the color of hyperlink text without underlining it on hover

I have a menu set up on my website that currently underlines when hovered over. However, I would like it to change color instead, which is the default style for my Wordpress theme. The title, "BOLI STYLUS," changes color exactly as I want it to. Below is ...

Unable to position the button next to the search bar

I'm struggling to get my search bar aligned next to my dropdown button. I've tried various alignment methods without success. Both buttons are within a container, causing the search bar to span 100% of the container's width. I also attempted ...

Tips for arranging the items in a dropdown menu side by side

As I work on creating a multi-level navbar using CSS and HTML, I am facing a challenge. I want to structure a drop-down ul-list with multiple columns side by side rather than in a single long column as is traditional. I have tried using the display: flex; ...

Issue with Bootstrap 5 Modal Overlay

I created a function that automatically generates a modal to save time and keep the code organized. This way, the HTML and JS related to the content of the modal are all in one PHP file, rather than mixed in with the file calling the modal: function new_mo ...

Utilizing Flexbox for Nested Columns

I am attempting to nest flexbox columns inside a flexbox layout. This is the HTML code I have: <div class="container"> <div class="row flex height"> <div class="col-md-6 red"> </div> <div class="col-md-6 orange"> ...