Is it possible for me to remove the style using getAttribute?

Recently, I have been working on developing a drag and drop game where the goal is to set the original value to none once the items are dropped correctly.

function dragStart(ev) {
  ev.dataTransfer.setData("Text", ev.target.id);
}

function allowDrop(ev) {
  ev.preventDefault();
}

function drop(ev) {
  event.preventDefault();
  var data = ev.dataTransfer.getData("Text");
  var dropdata = ev.target.getAttribute("drop-id")
  if (data == dropdata){
   ev.target.appendChild(document.getElementById(data))
   a = ev.target.getAttribute("drop-id");
   a.style.display = "none";}
}
.App{
    display: flex;
    flex: 1;
    flex-flow: column;
}

.App .Box{
    display: flex;
    flex: 1;
    width: 100%;
}

.App .Box .Boxes{
    display: flex;
    flex: 1;
    flex-flow: column;
}

.AM {
    border: 3px solid black;
    border-radius: 10px;
    background-color: orange;
    padding: 5px 5px;
    margin: 4px 0px;
    width: 90%;
}

.Example {
    border-style: Dashed;
    border-color: grey;
    border-radius: 10px;
    width: 90%;
    padding: 5px 5px;
    margin: 4px 0px;
}

.DB{
    border: 3px solid black;
    border-radius: 10px;
    background-color: green;
    padding: 5px 5px;
    margin: 4px 0px;
    width: 90%;
}
        <div class = "App">
            <div class = "Box">
                <div class "Boxes">
                    <div class = "AM">This is A</div>
                    <div class = "AM">This is B</div>
                    <div class = "AM">This is C</div>
                    <div class = "AM">This is D</div>
                    <div class = "AM">This is E</div>
                </div>

                <div class = "Boxes">       
                <div class = "Example" ondrop = "drop(event)" ondragover = "allowDrop(event)" drop-id = "1">Drop here to match</div>
                <div class = "Example" ondrop = "drop(event)" ondragover = "allowDrop(event)" drop-id = "2">Drop here to match</div>
                <div class = "Example" ondrop = "drop(event)" ondragover = "allowDrop(event)" drop-id = "3">Drop here to match</div>
                <div class = "Example" ondrop = "drop(event)" ondragover = "allowDrop(event)" drop-id = "4">Drop here to match</div>
                <div class = "Example" ondrop = "drop(event)" ondragover = "allowDrop(event)" drop-id = "5">Drop here to match</div>
                </div>
                
                <div class = "Boxes">
                <div class = "DB" draggable = "true" ondragstart="dragStart(event)" id = "1">A</div>
                <div class = "DB" draggable = "true" ondragstart="dragStart(event)" id = "2">B</div>
                <div class = "DB" draggable = "true" ondragstart="dragStart(event)" id = "3">C</div>
                <div class = "DB" draggable = "true" ondragstart="dragStart(event)" id = "4">D</div>
                <div class = "DB" draggable = "true" ondragstart="dragStart(event)" id = "5">E/div>
                </div>
            </div>
        </div>

After successfully dropping the items, I aim to make the "Drop here to match" area divisible. I tried using getAttribute() method to achieve this but unfortunately was unable to set it to 'none'. Is there any alternative way to use getAttribute() in order to apply styles like display : none or visibility : hidden? Your insights would be greatly appreciated.

Answer №1

To conceal the designated item, simply update the Drop function as shown below:

function drop(ev) {
    event.preventDefault();
    var data = ev.dataTransfer.getData("Text");
    var dropdata = ev.target.getAttribute("drop-id")
    if (data == dropdata){
        ev.target.appendChild(document.getElementById(data))
        ev.target.style.display = "none";
    }
}

Answer №2

function initiateDrag(ev) {
  ev.dataTransfer.setData("Text", ev.target.id);
}

function allowDropAction(ev) {
  ev.preventDefault();
}

function completeDrop(ev) {
  event.preventDefault();
  var data = ev.dataTransfer.getData("Text");
  var dropdata = ev.target.getAttribute("drop-id")

  if (data == dropdata){
   ev.target.appendChild(document.getElementById(data))
   a = ev.target.getAttribute("drop-id");
   
   ev.target.style.display = "none";
   document.getElementsByClassName('AM')[a - 1].style.display = "none";
   
}
}
.App{
    display: flex;
    flex: 1;
    flex-flow: column;
}

.App .Box{
    display: flex;
    flex: 1;
    width: 100%;
}

.App .Box .Boxes{
    display: flex;
    flex: 1;
    flex-flow: column;
}

.AM {
    border: 3px solid black;
    border-radius: 10px;
    background-color: orange;
    padding: 5px 5px;
    margin: 4px 0px;
    width: 90%;
}

.Example {
    border-style: Dashed;
    border-color: grey;
    border-radius: 10px;
    width: 90%;
    padding: 5px 5px;
    margin: 4px 0px;
}

.DB{
    border: 3px solid black;
    border-radius: 10px;
    background-color: green;
    padding: 5px 5px;
    margin: 4px 0px;
    width: 90%;
}
<div class = "App">
            <div class = "Box">
                <div class "Boxes">
                    <div class = "AM">This is A</div>
                    <div class = "AM">This is B</div>
                    <div class = "AM">This is C</div>
                    <div class = "AM">This is D</div>
                    <div class = "AM">This is E</div>
                </div>

                <div class = "Boxes">       
                <div class = "Example" ondrop = "completeDrop(event)" ondragover = "allowDropAction(event)" drop-id = "1">Drop here to match A</div>
                <div class = "Example" ondrop = "completeDrop(event)" ondragover = "allowDropAction(event)" drop-id = "2">Drop here to match B</div>
                <div class = "Example" ondrop = "completeDrop(event)" ondragover = "allowDropAction(event)" drop-id = "3">Drop here to match C</div>
                <div class = "Example" ondrop = "completeDrop(event)" ondragover = "allowDropAction(event)" drop-id = "4">Drop here to match D</div>
                <div class = "Example" ondrop = "completeDrop(event)" ondragover = "allowDropAction(event)" drop-id = "5">Drop here to match E</div>
                </div>
                
                <div class = "Boxes">
                <div class = "DB" draggable = "true" ondragstart="initiateDrag(event)" id = "1">A</div>
                <div class = "DB" draggable = "true" ondragstart="initiateDrag(event)" id = "2">B</div>
                <div class = "DB" draggable = "true" ondragstart="initiateDrag(event)" id = "3">C</div>
                <div class = "DB" draggable = "true" ondragstart="initiateDrag(event)" id = "4">D</div>
                <div class = "DB" draggable = "true" ondragstart="initiateDrag(event)" id = "5">E</div>
                </div>
            </div>
        </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

"Upon loading an FBX file in Threejs, some model parts are not displayed

Hello, I am in need of assistance. I am currently working on importing FBX models into Threejs and here is the import code that I am using: let loader = new FBXLoader(); loader.load(model.obj_path, object => { let mix = new THREE.AnimationMixer(objec ...

What is the method to adjust the time of print's font size?

When trying to print a datatable using the following JavaScript code: <script type="text/javascript"> function data(divName) { var printContents = document.getElementById(divName).innerHTML; var originalContents = document.body.innerHTML ...

Box-free calendar dash component

Encountering an issue within my application dashboard, here's a direct screenshot: https://i.stack.imgur.com/sEMrM.png The problem is that the Calendar days are extending beyond the borders of the calendar box. To implement this, I simply utilized ...

Greeting message in jQuery that only disappears when manually closed (for Rails 3)

How can I create a welcome message for first-time users in Rails 3 that remains visible until the user closes it once using jQuery's hide method? ...

What steps can I take to prevent my menu items from overlapping in the mobile navigation menu?

I am currently working on creating a mobile menu, but I'm facing an issue where the menu items overlap when hovered over. Instead, I want the menu items to move downwards when hovered upon to prevent the text from overlapping. Below is the HTML code ...

High-quality images appear fuzzy on Android devices while remaining clear on iPhones when using Lightview modals

The images in my Lightview modals, displayed as .png and .jpg, appear blurry on Android devices but not on iPhones. The modals and images are automatically sized based on the viewport dimensions. HTML (Generated by Lightview) <div class="lv_window"> ...

When utilizing VueJs, it's not possible to retrieve a data property from within a function

I am encountering a challenge when trying to access the data property within the function. Despite my efforts, I seem to be missing something crucial and unable to pinpoint what it is. Here is my class: export default { name: "Contact", component ...

Using Node.js and React to dynamically render a different HTML file in response to a request

We are currently working on implementing AMP pages for our project. Our current solution involves detecting a query in the URL, such as: myurl.com?amp=1, and completely redrawing the page with the necessary markup. However, our server is set up to choose ...

Issue with IE11: the selected list is not displayed when using the <s:optiontransferselect> tag

When moving groups from left to right in the s:optiontransferselect for selectedGrps and unselectedGrps, the SelectedGroups list is showing as null on form submission in IE11. However, in Chrome and Mozilla, it functions correctly. Any advice would be grea ...

Steps for developing a web-based interface for my software

I could use some guidance on how and where to get started. Currently, I have a program written in vb.net that performs basic functions by executing bat files, accessing specific folders, and carrying out command line tasks. My plan is to convert this progr ...

Tracking progress in a Rails job using Coffeescript

Recently, I came across a helpful gem called this gem, which allows for the seamless integration of bootstrap progress bars and the delayed job gem. The example provided by the creator uses .haml files, but since my project utilizes erb and coffeescript, I ...

I am struggling to comprehend the data organization illustrated by the typescript type declaration

type DocumentData = { [field: string]: any }; let data1: DocumentData = {4:3}; console.log(data1); //{4:3} It appears that the DocumentData type in the code above defines an object type where the key is of string type and the value can be of any type. Th ...

What is the proper way to showcase an image within an <li> tag?

So, here's the issue. I initially set up a standard "Home" Button that links to the Home Page of the website, but I felt that the word "Home" was too plain. As a solution, I attempted to replace the word with an icon, but unfortunately, it's not ...

"Struggling with getting the text color to change on hover in Tailwind

Today has been a frustrating day as I am diving into the world of tailwindcss and have been struggling with a particular issue. My code is below: <button className="bg-yellow-500 px-4 py-2 hover:text-black text-white"> Some Text Her ...

Processing requests through Axios and Express using the methods GET, POST, PUT, and DELETE

When working with express router and Axios (as well as many other frameworks/APIs), the use of GET/POST/PUT/DELETE methods is common. Why are these methods specified, and what are their differences? I understand that a GET request is used to retrieve dat ...

The methodology of executing a Javascript function

I have the following code snippet: (function() { $(document).ready(function() { //Event handlers $('.project-delete').on('click', function() { deleteProject($(this)); }); $('.keyword-delete').on(&ap ...

Clickable boxes for selecting options (Ajax and JavaScript)

I have a new project that involves creating a checkbox and using ajax and javascript to change its state when clicked. The goal is for the checkbox state to be saved in a php program that generates a .txt file. The state should be 0 when not clicked and 1 ...

Validation error detected in the textbox fields

I have incorporated Bootstrap into my application. For a specific functionality, I am dynamically creating dropdown and textbox controls based on a count and implementing validation. However, the validation seems to be malfunctioning only for the textbox ...

CSS experts caution that the addition of margin and padding can cause the screen to jump

When I apply margin and padding to this class, it starts glitching like in the second GIF. However, if I remove margin and padding everything works fine, but I do need them for styling purposes. What could be causing this issue and how can I resolve it? ...

Utilizing Web Worker to Load Textures in Three.js

When a large texture image is applied to a Mesh in Three.js for a substantial amount of time, it causes the browser's main thread to freeze. Consider the following scenario: var texLoader = new THREE.TextureLoader(); texLoader.load('someLargeTe ...