Using JavaScript, implement CSS styling to the currently active item that has been dropped within a designated drop

When dragging and dropping nodes, I want to ensure that only the dropped node is highlighted with a border. For example, if I drop nodeA, it should be highlighted. Then, when I drop nodeB, nodeB should be highlighted while nodeA is not.

Below is the function I wrote to achieve this:

function drop(ev) {
    ev.preventDefault();
    var data = ev.dataTransfer.getData("text");
    var nodeCopy = document.getElementById(data).cloneNode(true);
    test++;
    nodeCopy.id =test; 
    var newNodeId= nodeCopy.id;
    ev.target.appendChild(nodeCopy);
    document.getElementById(newNodeId).className += " draggeddStyle";
}

I am successful in highlighting the node using:

document.getElementById(newNodeId).className += " draggeddStyle;

However, after dragging another node, both nodes end up having the same style which is not desired.

Answer №1

Here is a revised version of your code snippet.

function drop(ev) {
    ev.preventDefault();
    $(".draggeddStyle").removeClass("draggeddStyle");/* included this line */
    var data = ev.dataTransfer.getData("text");
    var nodeCopy = document.getElementById(data).cloneNode(true);
    test++;
    nodeCopy.id =test; /* Different IDs must be used */
    var newNodeId= nodeCopy.id;
    ev.target.appendChild(nodeCopy);
    document.getElementById(newNodeId).className += " draggeddStyle";
}

This should do the job!

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 Firebase 'not-in' operator is malfunctioning

Within my database, there is a document located at: https://i.stack.imgur.com/OTZpd.png I am attempting to query the number of messages documents where the user's ID does not appear in the "read_by" array. This is the code I am currently using: const ...

Retrieve the date for the chosen time slot by utilizing the fullCalendar feature

I've been experiencing issues with a piece of code that is supposed to retrieve the date corresponding to a user-selected slot. Here's what I've tried so far: $('.fc-agenda-axis.fc-widget-header').on('mousedown', functio ...

Adjust the radio button's color within an ngFor loop when it is selected

When I select an item from my radio button list, I want to change its appearance by altering the background-color, text color, or highlighting. Unfortunately, due to the use of an ngFor loop for rendering the items, I am unable to achieve this as desired. ...

How can I make a GET request using jQuery/ajax?

I'm currently working on loading data from a MongoDB database hosted on Mongolab. The documentation is comprehensive, but I find it strange that there are examples for PUT and POST requests, yet there's no mention of GET requests: Is there anyon ...

Verify the validity of a form using JavaScript and by utilizing HTML5 validation, then proceed to save the form data in local storage

I'm fairly new to working with JavaScript and jQuery. Currently, I am attempting to validate the HTML5 validation of a form using JavaScript. When the form is valid, I want to save the data in localstorage WITHOUT having to reload the webpage. Here is ...

Exploring the potential of overflow within a container with a percentage-based

I am attempting to design a webpage that fills the entire screen height without scroll bars, yet contains a scrollable container within it. Despite trying various approaches with percentage heights, I have not been successful as the percentages do not beha ...

Obtaining the selected option from a dropdown list using JavaScript

How can I retrieve the value of a drop-down list and use it in a switch statement? var selectedValue = document.myform.sel2.value; var result; if(document.myform.sel1[0].selected){ switch(selectedValue){ case document.myform.sel2[1].selected : ...

What is the best way to align a button at the bottom using Bootstrap 4?

My question is not as straightforward as the title suggests, but I couldn't think of a better one. Initial Configuration: I am using Bootstrap v4.0.0-alpha.2 and I removed this simple sidebar. I also added flex: true in my _library-variable-overrides ...

Having issues with IE8 and SmoothGallery script errors

I'm currently utilizing the SmoothGallery plugin created by Jon Designs to enhance the website of one of my clients. However, there seems to be a minor issue in Internet Explorer 8 when attempting to navigate to the next image. Interestingly enough, a ...

Exploring JavaScript's capability to parse JSON data

Having some trouble extracting information from the Google Maps API into my application using JavaScript. I'm struggling to figure out how to access the data in the returned JSON object. var site = "./maps/scripts/reverseGeocode/locale.php"; ...

Could the issue be related to a bug in the combination of ng-repeat and ngInclude?

I've been experimenting with loading different templates in this manner: <div ng-class="{active:$first,in:$first,'tab-pane':true}" id="{{p.path}}_settings" ng-repeat="p in panes" ng-include="buildPath(p.path)"> </div> Here&apos ...

The Bootstrap modal fails to open

I am currently working on implementing a Navbar button that triggers a Bootstrap modal containing a form. While looking for resources, I stumbled upon a useful script at https://gist.github.com/havvg/3226804. I'm in the process of customizing it to su ...

Using 2 parameters, ExternalInterface.addCallback

Attempting to pass 2 variables from JavaScript to Flash, I came up with a solution in ActionScript 3 to try and receive them. ExternalInterface.addCallback("callAs", muscle, tension); However, this method resulted in an error: Error: 1137 - Incorrect nu ...

Warning in Vue.js: Duplicate keys with the value of '0' have been detected, which could potentially lead to update errors. This issue was found within the Vue.js Bootstrap collapse

When trying to create a collapsed card with Bootstrap 4 and Vue.js, I encountered some issues. Bootstrap 4 was functioning properly without Vue.js, maintaining the proper transitions and not expanding cards that were not clicked. However, after moving my c ...

Using JavaScript code to dynamically display the value of the variable MyProperty in HTML

Are there any limitations when using this construction in JavaScript? In my codeBehind, there is a property with intricate logic in the get method. It calls other methods and despite debugging showing a good value, it returns an empty string. I'm curi ...

The bootstrap script did not complete loading before the initialization of the tooltip

My one-page website utilizes Bootstrap 5 with Spring Boot and Thymeleaf. I am encountering an issue where the initialization of tooltips in Bootstrap is happening before the script has fully loaded, causing errors and disrupting the page loading process. ...

Using FUELPHP to make Ajax requests without relying on jQuery

After conducting thorough research, I have come to the conclusion that FUELPHP does not handle Ajax requests as efficiently and natively as some other frameworks like RubyOnRails. It seems that in order to perform Ajax requests with FUELPHP, one must manu ...

Step-by-step guide on generating a PDF document using an OpenLayers div

I'm struggling to figure out how to generate a pdf file with open layers. Within a div, I have WMTS tiles generated using openlayers. My goal is to create a pdf file for my users from these tiles, but I haven't been able to find a solution onlin ...

What is the reason that the keyword 'in' does not function for verifying the presence of a particular word within an array in JavaScript?

Currently, I am focused on string manipulation for a chatbot. My main objective is to identify a specific word within a returned string message. Here is an example of my approach: let msg = 'how are you?' //An illustration of ...

Failing to reach the nested if statements within a switch case block

Before removing my question, please read this. Despite testing with console.logs, my code does not enter the if statements. I have not come across a similar solution to my issue. In an attempt to address any timing or asynchronous problems, I added a use ...