What steps can I take to address this particular issue with Javascript/CSS?

I need to figure out how to get the smaller div box to display properly within the larger div.

Every time I try to drag and drop it, the result is not what I expect:

What could be causing the border of the small box to not show correctly in the larger box? I even adjusted the drop function so that it copies the 2nd div instead of just moving it.

<html>
    <head>
        <script>
            function remove(id){
                //var el = document.getElementById("r1").outerHTML = "";
                var element = document.getElementById(id);
                element.parentNode.parentNode.removeChild(element.parentNode);
                console.log('Removed')
            }

            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");
                var nodeCopy = document.getElementById(data).cloneNode(true);
                nodeCopy.id = 'something';
                ev.target.appendChild(nodeCopy);
            }
        </script>
    </head>
    <body>
        <p>Try dragging the W3Schools image into the rectangle:</p>
        <div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
        <br>
        <div id = "drag1" draggable="true" ondragstart="drag(event)" height="50px" width="50px">
            <span id="yo" class="fa fa-close cross" onclick="remove(this.id);"></span>
        </div>
    </body>
</html>

Answer №1

Hello @Presence, based on my analysis of the provided HTML and JS code, it seems that there is no issue with your javascript implementation. The problem you are encountering might be related to CSS styling.
You have the option to use the suggested CSS solution mentioned in this response. Alternatively, you can share your CSS code for further investigation to pinpoint the specific property causing the issue.

function remove(id) {
  //var el = document.getElementById("r1").outerHTML = "";
  var element = document.getElementById(id);
  element.parentNode.parentNode.removeChild(element.parentNode);
  console.log("Removed");
}
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");
  var nodeCopy = document.getElementById(data).cloneNode(true);
  nodeCopy.id = "something";
  ev.target.appendChild(nodeCopy);
}
.dropDiv {
   height: 15vh;
   border: 2px solid grey;
}

.dragDiv {
   height: 9vh;
   border: 2px solid grey;
   width: 53vw;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <title>Document</title>
</head>

<body>
    <p>Drag the W3Schools image into the rectangle:</p>

    <div class="dropDiv" id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <br>
    <div class="dragDiv" id="drag1" draggable="true" ondragstart="drag(event)" height="50px" width="50px">
        <span id="yo" class="fa fa-close cross" onclick="remove(this.id);"></span>
    </div>
    <script src="./index.js"></script>
</body>

</html>

Answer №2

height="50px" width="50px" is not compatible with the div element. It only works with specific tags.

You can apply styles to the div in the following ways:

  1. Add CSS styling using a class (recommended method).
  2. Add in-line styling to the div, for example: style="width: 100px"

Below is the code snippet for your reference.

function remove(id){
    //var el = document.getElementById("r1").outerHTML = "";
    var element = document.getElementById(id);
    element.parentNode.parentNode.removeChild(element.parentNode);
    console.log('Removed')
}

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");
    var nodeCopy = document.getElementById(data).cloneNode(true);
    nodeCopy.id = 'something';
    ev.target.appendChild(nodeCopy);
}
.box{
    border: 1px solid black;
}

.big{
    width: 120px;
    height: 50px;
}

.small{
    width: 80px;
    height: 30px;
}
<p>Drag the W3Schools image into the rectangle:</p>
        
<div id="div1" class="big box" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<br>
<div id="drag1" class="small box" draggable="true" ondragstart="drag(event)">
    <span id="yo" class="" onclick="remove(this.id);">x</span>
</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

Can you explain the process of extracting images from JSON data using AJAX and jQuery?

Hello, I'm looking for guidance on incorporating jquery with AJAX to fetch images from a JSON file and showcase them on my website. Below is the code snippet I have put together. function bookSearch(){ var search = document.getElementById('sea ...

How to visually deactivate a flat button ( <input type="button"> ) programmatically with JavaScript

I am facing an issue with my buttons. I have one regular button and another flat button created using input elements. After every click, I want to disable the buttons for 5 seconds. The disable function is working properly for the normal button, but for th ...

Unable to show inline within web forms application utilizing bootstrap

I am currently modifying the account registration section of a new VS 2013 web application that uses bootstrap css for formatting. I am working on creating a form on a page and struggling to get one section to display inline instead of as a block element. ...

Ways to conceal overflow at the base of a container while enabling the image to break free from the top of the container

Trying to create a visual effect where an image of a person appears to be partially hidden inside a container, with the bottom part concealed by overflow: hidden. However, I want the top part of the image to extend out of the container. Is there a way to a ...

Error: The variable 'error' could not be located

Below is the code that I am using: $(document).ready(function(){ var callPage = function(){ $.post('/pageToCall.php'); }; setInterval('callPage()', 60000); }); However, I encountered an error stating ReferenceErro ...

unfamiliar gap in the absence of jquery's creation

Trying to create a form similar to this example: https://jsfiddle.net/zycyuyrz/ The issue is that the HTML code for static rows and dynamically generated jQuery rows appears different. Any idea why? HTML: <div class="container"> <fieldset ...

"Encountered a TypeError stating that Object(...) is not being recognized as a function" while attempting to import a function via a barrel

In one of my files, I have a function named merge which has the following code inside: export default (prev, next) => Object.assign({}, prev, next) Additionally, there is a barrel file that imports and exports this function like so: import fetchToNode ...

What is the process for translating HTML elements into JSX format?

I have a couple of inquiries. 1.) I am working on a basic reactjs component. How can I transform it into JSX? I want the code to follow the reactjs style. 2.) If I receive an array from the backend in this format [ '/parent-folder-1/child-folder ...

Achieving Center Alignment of Two Elements with Varying Widths in Relation to the Parent Container, all while maintaining Left Alignment and consistent indentation

I am currently working with Material UI, but maybe this is just a matter of understanding how to achieve it with Flexbox. Something similar to what is shown in the image below. I am struggling to make all three lines align in the same way on different scre ...

Exploring the capabilities of Angular 2 and delving into inquiries regarding IIS

I'm diving into the world of Angular 2 as a beginner. Previously, I used to include JavaScript (like jQuery.js) in my HTML and then upload the finished page to my IIS website. Now that I'm learning Angular 2, I've had to install Node.js, NP ...

Issue with Ruby Selenium Webdriver: Unable to locate attributes for AngularJS Radio Buttons

I am having trouble detecting the presence of the "checked" attribute on a radio button. Here are the two radio buttons in question: Radio Buttons These are the attributes of the elements: <input type="radio" ng-change="logConsent(true)" ng-model="xx ...

Incorporating personalized CSS into Drupal 7 for concealing the notice

Utilizing my custom block to showcase a flash game on the main page of my Drupal 7 setup, I encountered an irksome message: <div id="first-time"><p>No front page content has been created yet.</p> <div class="item-list"><ul>&l ...

Enhance the current menu item in WordPress by assigning a class to the anchor tag

function updateActiveClassOnMenu($item_output, $item, $depth, $args) { $menu_locations = get_nav_menu_locations(); if ($item->menu_order == 1){ $item_output = preg_replace('/<a /', '<a class="active" ', $item_o ...

What is the best way to send put and delete requests through a form using node.js and express.js?

Attempting to send a put request: Put form code snippet: <form id="for" action="/people" method="post"> <div class=""> <input type="text" name="Name" value=<%= data[0].name %> > </div> ...

Replace the identifier with the corresponding ID if it aligns with another ID when using Node.js or JavaScript

const info = [ { system: { id: "4gSSbjCFEorYXqrgDIP2FA", type: "Entry", content: { type: { name: "Writer" } }, }, InfoDetails: { shortSlugOption: { "en-us": "some value", "za-op": "random value" }, mediaFileAsset ...

What steps must be taken to display a div element upon clicking an object or entity within an Aframe scene?

Experiencing some coding issues that I could use help with. As a newcomer to Javascript, I might be making a beginner's error here. My goal is for the red tree on the globe in my example to trigger a red div box when clicked. Despite my efforts, I kee ...

"Enhance your web development skills by mastering jQuery alongside the

It's curious that jQuery doesn't support the use of the "+" sign. You can see how it functions with "1" and "3", but not with "2+". Just hover your mouse over each div to experience it. <div id="div-2+"></div> JSFiddle $('a. ...

How to effectively handle form CRUD operations in Angular 2/4 using parent and child components

In my setup, there is a parent component along with a child component. The parent mostly consists of a list of repeating children created using *ngFor directive. Each child component receives an object through Angular's @Input feature, and the child ...

Have you ever wondered why Vue 3 imports all JS files for every page upon the initial project load?

Is there a way to make Vue 3 import only the necessary js files for each component instead of loading all files at once when the project is first loaded? For example, if I navigate to the contact page, Vue should only import the contact.js file, rather tha ...

Using a physical Android device to test and run a Meteor mobile application

I'm struggling to get my Meteor app to run on my Android device (LG G2). Despite searching online for a solution, I haven't come across any similar issues. I followed the instructions carefully, added the Android platform to my project, and ran i ...