Styling a specific <div> element that contains multiple nested <div> elements sharing

Having multiple divs with the same class poses a challenge. When a user taps on one div, a slidetoggle() function reveals two buttons - "accept" and "reject". The goal is to change the background color of that specific div to green or red based on the button clicked.

The issue arises when attempting to assign colors as all divs end up changing color simultaneously.

Is there a way to target and change the color of only the specific div?

$(document).on("pagecreate","#one1",function(){
    $("div.comp2").on("tap",function(){
        $("#panel").slideToggle("slow", function(){
            $("#accept").on("click",function(){
                $(this).closest('div').css("background-color","#22bb45"); 
            });
        });
    });
});

Answer №1

When handling an event, you have the ability to use the this keyword to specifically target the element that triggered the event, rather than having to select all elements based on a class. Here is an example:

$(document).on("pagecreate","#one1",function(){
    $("div.comp2").on("tap", function() {
        var $comp2 = $(this);
        $("#panel").slideToggle("slow", function(){
            $("#accept").on("click", function(){
                $comp2.css("background-color","#22bb45"); 
            });
        });
    });
});

Answer №2

$(document).on("pagecreate","#one1",function(){
    $("div.comp2").on("tap",function(){
        var selectedElement = this;
        $("#panel").slideToggle("slow", function(){
            $("#accept").on("click",function(){
                $(selectedElement).css("background-color","#22bb45"); 
            });
        });
    });
});

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

Unable to render the JSON data that was retrieved from a jQuery AJAX request

I am having trouble displaying JSON data that is returned from an AJAX call. Can someone please assist me? I am new to this. $.ajaxSetup({ cache: false, timeout: 5000 }); //String.prototype.toJSON; var the_object = {}; function concatObject(obj) { ...

Issues with UA PhoneGap 2.0 plugin failing to initialize properly on iOS device

In my attempt to integrate push notifications into my iOS PhoneGap 2.0 app using the recently released Urban Airship plugin, I encountered an issue. Everything functions perfectly when I load the index.html from the provided sample application into my proj ...

Activate the script upon the left-click of the arrow icon

Looking for help with this basic javascript code snippet. $('#about_us').on('click',function() { $('#slider').toggleClass('open'); }); I'm trying to find a way to activate this function by pressing the lef ...

Is it feasible to capture a screenshot of a URL by using html2canvas?

Is it possible to take a screenshot of a specific URL using html2canvas? For example, if I have the following URLs: mydomain.com/home mydomain.com/home?id=2 mydomain.com/home/2 How can I capture and display the screenshot image on another page? window ...

Update the page when the Cancel prompt is selected

Issue: I am facing a problem with my web page. After entering data and clicking save, a confirmation pop-up appears. If I choose to cancel, I want the page to refresh so that the entered data is cleared. Below is the current code for the confirmation pro ...

Align the button at the center of the carousel

While working on a personal HTML/CSS/Bootstrap 5 project as a self-taught learner, I have encountered some beginner doubts. My challenge is to ensure that the site remains responsive across various devices such as mobile and tablet. Specifically, I am stru ...

To prevent the animation from overflowing, set the parent element to have hidden overflow while still displaying the child element

I am facing an issue with two menus that can be toggled using a switch. Each menu item has a dropdown that appears when clicked. The problem arises when switching between the menus, as there is an animation for the transition (slide in and slide out). I wa ...

The AJAX functionality seems to have broken following the switch from php5 to php7

When I initially wrote my code in php5, the index page would make an ajax call to check if $_SESSION['user'] was stored. If a session existed, the user's information would be displayed; otherwise, the page would redirect to the login page. H ...

Guide on how to import or merge JavaScript files depending on their references

As I work on my MVC 6 app, I am exploring a new approach to replacing the older js/css bundling & minification system. My goal is to generate a single javascript file that can be easily referenced in my HTML. However, this javascript file needs to be speci ...

Did the IBM MobileFirst client miss the call to handleFailure?

I am currently utilizing the IBM MFP Web SDK along with the provided code snippet to send challenges and manage responses from the IBM MobileFirst server. Everything functions properly when the server is up and running. However, I have encountered an iss ...

Tips for storing mustache templates for rendering in Node.js

My data is stored in the following format: let data = {"list" :[ { "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="98f9fafb8afef0f9f5e8f4fdb6fbf7f5">[email protected] ...

The EJS file is failing to display the stylesheet even though it is being pulled from the

Encountering a strange issue where the page routed to display additional information about a specific record from my database list on the homepage is not loading the stylesheets located in my partial/head, despite successfully passing the object informatio ...

Using jQuery to temporarily disable a div element

I need to implement a timer in a div that will disable it for a specific duration, such as 3 seconds. Here is the HTML code snippet: <div class="answ-container" align="center"> <div class="c_answer" id="1316" name="1" data-rcat-no="1"> ...

Example of VPAID pre-roll ads

I've been searching for a VPAID code example to use in a sample preroll ad for quite some time now, but I haven't had any luck finding one. If anyone has a working example, could you please share it with me? Thank you! By the way, I am using vid ...

unable to establish a secure connection to the specified URL within my application

I am facing an issue with my form that includes hidden values which need to be sent to a URL upon submission. The URL is for secure card payment purposes, and when I try to process it through my app by opening the URL and sending the hidden values, the con ...

Sending information from Ajax to PHP

Could anyone please explain the script provided above to me? I am attempting to pass the value of $user data so that I can utilize $_REQUEST['user'] within sort.php, but I am encountering difficulties. It seems to be passing in a long URL. $(fun ...

An issue has arisen where FormData fails to send the value of a

My current issue involves submitting a form using FormData. Interestingly, all input types are functioning as expected except for checkboxes. When the checkbox value is set to 1 or 0, Ajax fails to post it. <form id="update-form" method="PUT" enctype= ...

Organize the array of objects

Within my React state, I aim to rearrange a set of 3 objects by always placing the selected one in the middle while maintaining ascending order for the others. Currently, I utilize an order property within each object as a way to keep track of the sequenc ...

A tutorial on implementing a "Back to Top" button that appears dynamically while scrolling in Angular

I've developed a scroll-to-top feature for my Angular project, but I'm facing an issue. The scroll icon appears immediately upon page load instead of only showing after the user scrolls down. Any ideas or suggestions on how to achieve this? Here ...

The process of extracting a value from an array of objects encountered an error due to the undefined object

I am looking to extract the value from an array within an object while also implementing error checking. The code I currently have checks if a specific key exists in the object and if the value associated with that key is of type array. If both condition ...