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

Error in syntax: An unexpected token was encountered during an AJAX request

I am currently working on a project that involves looping through multiple JSON files within a directory called "trips" and extracting data from each file to display on the front end of the website. However, I'm encountering a persistent error message ...

JavaScript Promise Chains- Issues with Functionality?

Could someone provide an explanation for why calling secondMethod in the Promise chain yields results, while calling secondMethod() does not? function firstFunction() { return new Promise(function(resolve, reject){ setTimeout(function() { ...

Adjust the width of a float-right container to its maximum width

Looking for a solution similar to the one discussed in this question: CSS - Float to max width In my project, I am working on a layout that involves a <pic> / <info> style setup, with a twist - I want it to be responsive when the user resizes ...

Is there a solution to resolve the Firestore issue stating: "FieldPath.documentId is not a recognized function"?

In my function, I am retrieving data from two collections in Firestore: Media and Users. Inside the Users collection, there is a subcollection containing a list of all the user's movies. The Media collection includes details about each movie. My goal ...

Discovering the exact latitude and longitude coordinates along a specific route using Google Maps

I've encountered a problem with finding the latitude and longitude along a given route using Google Maps DirectionsService. I have a JSON dataset containing latitude, longitude, and price details. My goal is to plot markers on the map and determine wh ...

Utilize JSON data to dynamically populate TextFields or Select menus depending on a specific key in the JSON object

As I retrieve multiple JSON groups from an API, each group contains one or more questions objects. The task at hand is to attach each question along with its corresponding response to a textField. Based on the value of QuestionType, it will be decided whet ...

jQuery inserts a closing tag before reopening it when applying .before()

I have the following HTML code: <ul> <div> <li>a</li> <li>b</li> <li class="break">c</li> <li>d</li> <li>f</li> ...

Web scraping with Cheerio in Node.js sometimes yields undefined results

When attempting to extract data from NSE website, I initially tried using the inspect element console: (Edited the question) objs = $('div[class="table-wrap"] > table > tbody > tr > td').slice(0, 8) objs.map((i,element) =& ...

Guide on transferring Context to the loader in React-Router-6

In my development setup, I am utilizing Context for managing the global loading state and React-router-6 for routing. My approach involves incorporating loader functionality in order to handle API requests for page loading. However, a challenge arises when ...

Retrieving data from an HTML webpage using VBA

In my attempt to access the "username" cell from a web page using VBA, I encountered an issue. The HTML code of the page contains multiple elements with the same name, "LOGON_USERID", making it challenging for me to identify and access the correct one. Hi ...

Using a function with a parameter as an argument in an event handler

Imagine you have the code snippet below: $('#from').focus(listExpand(1)); $('#to').focus(listExpand(3)); I am facing an issue as the code is not behaving as expected. I believe the problem lies in passing a function result instead of ...

JavaScript - Modify the proposed content prior to inserting it into the input field

In my current project, I have implemented a feature using jQuery UI - v1.11.4, where an HTML textbox utilizes a JavaScript autocomplete functionality. The suggested string for the textbox is created by concatenating several columns (patient_no, patient_nam ...

Is it possible to display a complete list of my items along with their parameters using jQuery ajax?

My function code is set up to display all the items from my list of discipline codes, but I'm facing issues viewing them due to data:{params} always being present. alert('Could not retrieve data'); Any assistance would be greatly appreci ...

Preparing data before using Kendo UI upload function is essential

I need to pass a data (specifically a GUID) to the upload method of the kendoUpload, so that a particular MVC Controller action method can receive this data each time the upload occurs. $("#propertyAttachmentUpload").kendoUpload({ async: { ...

Having trouble selecting a button using xpath in Scrapy Python?

I am currently attempting to scrape quotes from a website called using my spider code that looks something like this: class quote(scrapy.Spider): name = 'quotes' # defining Name start_urls = [''] # Targeted urls def parse(s ...

An error occurred with the datepicker: Unable to connect to 'bsValue' as it is not recognized as a property of 'input'

Despite importing DatepickerModule.forRoot() in my Angular unit test, I am encountering the following error: Error: Template parse errors: Can't bind to 'bsConfig' since it isn't a known property of 'input'. (" ...

After submitting my form, the Bootstrap Modal does not hide as intended by my Ajax

I am facing an issue with my webpage that displays 6 Bootstrap Cards in 3 columns. Each card contains an image, name, description, and a footer button. When a user clicks on the button, a Bootstrap Modal opens with specific data fetched from a SQL row by I ...

Using jQuery's AJAX method: How to access a referenced element outside of a closure

Here is the code snippet I am using to make an ajax call: I am trying to figure out how I can access the target element. Declaring a target element does not seem to work, possibly because I am defining the function handler outside of the click event handl ...

Developing a personalized Hook with useRef functionality

Here is a code snippet where I demonstrate creating two custom hooks in React. The first hook, `useChangeText`, utilizes the `useState` hook and works as expected. The second hook, `useGetHTML`, attempts to use `useRef` to log an HTML element to the cons ...

Expanding your JavaScript skills: Tackling nested object key and value replacements

I am looking to manipulate the values of a nested object using JavaScript. The structure of the object is outlined below. let jsonObj = { "service":[ { "name":"restservice", "device&quo ...