Is it possible to modify the sub/child divs within a draggable parent div and assign them a different class?

Greetings, after being a long-time reader, I have finally decided to post for the first time. In the process of creating a webpage with jQuery drag and drop divs, I am curious about how to change the class of a child div within a draggable div once it is dropped onto a "droppable" div. Here's an overview of the HTML structure:

<div class="dropbox"></div>

The related jQuery script looks like this:

<script type='text/javascript'>//<![CDATA[
$(function(){
$("#draggable").draggable({
drag: function(event, ui) {
    $(this).removeClass('dropClass');
    }
});

Later in the script, the droppable section is implemented as follows:

$(".dropbox").droppable({
tolerance: 'intersect',
over: function(event, ui) {
    $('.ui-draggable-dragging').addClass('hoverClass');
},
out: function(event, ui) {
    $('.ui-draggable-dragging').removeClass('hoverClass');
},
drop: function(event, ui) {
    $('.ui-draggable-dragging').removeClass('hoverClass').addClass('dropClass');
}
});

If a child div of the draggable element has a class named "indicator," is there a way to modify the drop event function to also include the following code?

$('.indicator').removeClass('hoverClass').addClass('setOn');

I understand that the initial section creates the class for "draggable," which then becomes part of "ui-draggable-dragging." Is there a method to trigger another class like "indicator" without using an additional event when executing the drop function?

I hope this explanation is clear, as I have not found a solution to this issue elsewhere.

Answer №1

Give this JavaScript a shot...

$("#draggable").draggable({
    drag: function(event, ui) {
        $(this).removeClass('dropClass');
        $(this).children('.indicator').removeClass('setOn');
    }
});



$(".dropbox").droppable({
    tolerance: 'intersect',
    over: function(event, ui) {
        $('.ui-draggable-dragging').addClass('hoverClass');
    },
    out: function(event, ui) {
        $('.ui-draggable-dragging').removeClass('hoverClass');
    },
    drop: function(event, ui) {
        $('.ui-draggable-dragging').removeClass('hoverClass').addClass('dropClass');
        $('.ui-draggable-dragging').children('.indicator').addClass('setOn');
    }
});

Hopefully, you find this helpful..

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

Utilizing JQUERY to modify the 'top' attribute upon clicking

I am attempting to implement a vertical scroller using jQuery, but I'm encountering difficulties with my code. function scrolldown() { var newtop = $('.scroll-content').css('top') + '250'; $('.scroll ...

How to display a div in Angular when hovering with ElementRef and Query List

I am having trouble implementing a ngFor loop in my project where I want to display a div on mouse hover using the @Input notation. This is how my HTML code looks: <div class="col s12 m6" style="position: relative" *ngFor="let res of hostInfo.resident ...

Foundations of JQuery Ajax with GET Requests

Hi there, I'm back again. I'm currently trying to learn the basics of Jquery Ajax with some help here and so far, it's going well. However, sometimes the answers provided can be very specific to certain requests, which may not be as helpful ...

Live Server in VS Code not refreshing automatically as expected

My problem is with VS Code and Live Server for HTML CSS. I expected the page to automatically reload after editing my HTML, but that's not happening. Even though I have Auto-Save enabled, Live Server isn't updating or refreshing my page automati ...

Discovering the identical element within the ajax response

The following section is being targeted: var obj = $(this).parent().find('a'); // $(this) is a <UL> inside a <DIV>, but there can be multiple DIV>ULs on the page After that, I retrieve some HTML using $.ajax(). This HTML corres ...

Using Angular to send all form data to the API endpoint

Please bear with me as I am new and may ask small or incorrect questions. <form > <label for="fname">First name:</label> <input type="text" id="fname" name="fname"><br><br> <label for="lname">Last name:< ...

JavaScript for validating forms in PHP

Hey everyone, I'm struggling to understand why the alert box isn't showing up when I run this code. I'm new to programming and find HTML easy, but I'm currently in a PHP class where we have been tasked with creating and validating a for ...

Issue with jquery.validate.js in an MVC 4 Project using jQuery 1.9

Recently, I set up a new ASP.Net MVC 4 project using the default template in Visual Studio 2012. However, I encountered an issue after updating to jQuery 1.9 where the login functionality stopped working. The specific error message that appears is: 0x80 ...

Utilizing a dynamic value in an Angular directive

For my latest project, I am working on developing a basic JSON pretty-printer directive using angular.js. Here is the code snippet I have so far: (function(_name) { function prettyJson() { return { restrict: 'E', ...

The safari browser is not displaying the menu

I have recently been working on a new website and created a fresh menu for it. While the menu functions perfectly on browsers like Chrome and Firefox, I encountered an error when testing it on Safari. On Safari, the menu appears to be completely hidden. ...

The arrangement of pictures on a card

I decided to tweak the original concept by placing the images inside a Bootstrap card. However, the end result is that the images appear to be floating. https://i.stack.imgur.com/Uf721.png Any suggestions on how to make them fit nicely within the card? ...

Searching for $or command in express.js

Is it possible to use $or in the app.get() function of an express.js application? For example, how would I write a find() query that includes $or like this: db.inventory.find( { $or: [ { quantity: { $lt: 20 } }, { price: 10 } ] } ) I'm curious about ...

Managing form submissions using Jquery and checkboxes

Whenever I submit my dynamically created form, all checkboxes briefly become selected for a split second. Does anyone have an explanation for this behavior? Is it common or is there something wrong with my code? Although the form submits correctly, it migh ...

Issues with a jQuery ajax request

Recently delving into jQuery and finding it enjoyable so far. However, I've encountered an issue with an ajax call and could use some assistance. Here's the code snippet for my ajax call: // Initiating the ajax request $.ajax({ // PH ...

Leveraging $http or $timeout in conjunction with $stateProvider in AngularJS

I am seeking guidance on loading a template for a specific state in Angular using $http after coming across this question on Stack Overflow: Is it possible to load a template via AJAX request for UI-Router in Angular? The documentation for ui.router demon ...

There seems to be a problem with completing the Axios post request in the Javascript Front

My front-end, built with React, is having trouble retrieving data from a third-party API through my Node.js backend. Despite using axios.post request, the response remains undefined and I'm stuck at handling the asynchronous request. It appears that t ...

It never fails to function automatically, no matter which script is being executed

By default, the script will always be executed regardless of its environment. Check out my code snippet: import { Pool } from 'pg'; import config from './../config'; const connectionString = () => { switch (process.env.NODE_EN ...

Ways to identify a disconnected RTCPeerConnection

Currently, I am exploring a WebRTC demo on my Chrome browser and have successfully set up a video conference. However, I am facing an issue when one of the peers disconnects, such as refreshing the browser. I am unsure how to detect this disconnection on t ...

When trying to upload a file with ng-upload in Angular, the error 'TypeError: Cannot read properties of undefined (reading 'memes')' is encountered

Struggling with an issue for quite some time now. Attempting to upload an image using ng-upload in angular, successfully saving the file in the database, but encountering a 'Cannot read properties of undefined' error once the upload queue is comp ...

Having trouble with the JSON response while implementing AngularJS

Recently, I've started working with angularjs and ran into an issue where the data is not loading on the page when pulling JSON from a Joomla component. Strangely enough, everything works perfectly fine when I retrieve the data from a getcustomers.ph ...