Is it possible to adjust a single element within a class without affecting the rest using CSS?

Currently, I am facing an issue where my jQuery and JavaScript code is affecting all instances of messageCase when I only want to target and expand one specific instance. How can I modify my code to achieve this without impacting every occurrence of messageCase?

Additionally, it's important to note that I am working under "use strict" conditions.

$(document).ready(function () {
    var aTag = $("div.messageCase").id();
    var divTarget = $("div.messageCase")

    $(document).click(function (e) {
        var target = $(e.target);
        if (target.is(aTag)) {
            if (divTarget.hasClass("messageCase")) {
                // divTarget.removeClass("messageCase");
                divTarget.addClass("messageCase messageCaseAnimation");
            }
        } else {
            divTarget.addClass("messageCase");
        }

        if (!target.is(divTarget))
        {
            $(divTarget).removeClass("B");
        }
    });

});

Thank you for your help!

Answer №1

Within your click function, you are assigning the clicked element to the variable target. This means you could simplify by doing the following:

target.addClass("messageCase messageCaseAnimation");

Here is a basic example of how this works within your code:

$(document).ready(function() {
  var divTarget = $("div.messageCase")

  $(document).click(function(e) {
    var target = $(e.target);
    target.addClass("messageCase messageCaseAnimation");
  });

});

Additionally, here is a complete demonstration:

$(document).ready(function() {
  var divTarget = $("div.messageCase")

  $(document).click(function(e) {
    var target = $(e.target);
    // Remove the messageCaseAnimation from all
    divTarget.removeClass('messageCaseAnimation');
    
    // Add the messageCaseAnimation to only the clicked
    target.addClass("messageCaseAnimation");
  
  });

})
.messageCaseAnimation {
  background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="messageCase">
  Case 1
</div>

<div class="messageCase">
  Case 2
</div>

<div class="messageCase">
  Case 3
</div>

<div class="messageCase">
  Case 4
</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

Mastering jQuery ajax in Google Chrome Extensions

I have developed a script to fetch data from an external server using JSONP request in jQuery. Please take a look at the code snippet below: $("#submit").click(function() { var state = $("#state").val(); var city = $("#city").val(); $.ajax({ ...

Show automatically created forms alongside each other within a v-for loop

When generating dynamic forms from a json file, the forms end up being displayed one on top of the other like this: https://i.sstatic.net/tbdzs.png However, I want them to be displayed like this (for example, if there are 3 inputs, the fourth one should ...

Framework7: Reload the current page and add it to the page history stack

I'm working on a Cordova Android app with Framework7 integration. Is it possible to stack the same page multiple times in the page history? Imagine I'm currently on the "category.html" page. I need to navigate to nested subcategories, but sinc ...

Use jQuery to retrieve query string values and then locate elements that correspond to those values

Attempting to extract digit values from the query string using a regex. This will then be used to find elements in the DOM with a data-value attribute matching one of the regex results. Regex Remove allow=1 and paged=0 Select only value pairs with digi ...

Looking for a way to integrate a Facebook feed (RSS) into your webpage? Try using this jQuery

I am looking for a quick and easy way to display a Facebook feed (including posts, comments, and images if possible) on a website without using the standard 'like box' provided by Facebook. After some research, I came across this option: Are th ...

Unable to execute and map the array promise of a POST request

I am facing an issue with my API called getQuote and a component named QuoteCard. Within the QuoteCard, I am attempting to display an array of users who liked a specific quote. The API is functioning properly, as verified through testing, and the code snip ...

intersectionObjects malfunctioning

I've been struggling with this issue for a few days now, and I finally gave up and decided to reach out for help. My goal is to implement a simple object detection feature, but since the intersectScene method was replaced with intersectObjects (which ...

Encounter a critical issue while making a JSON request using Kendo UI

I am facing an issue at my workplace where I need to integrate Angular.js with ASP.NET MVC. Currently, I am trying to build a simple application that features a Kendo UI grid on the front page. In my App.js file, I am fetching data from the Data Controller ...

Rails implementation of autocomplete feature using sleek and seamless jquery integration

I've created a unique JavaScript code for a dropdown combo box feature that functions similarly to an autocomplete. I have added a button to the dropdown combo box, but I am struggling with figuring out the click event handler. The button should displ ...

A guide on adding specific rows together in a list

I'm working with a grid that contains various items and I want to calculate the total of the val_itemservico column only for the selected rows, not all of them. How can I achieve this functionality? When the user clicks on the "Calculate" button, I n ...

JavaScript used for making an AJAX request

Currently, I am attempting to master the art of making an AJAX call using plain JavaScript with the goal of stepping away from JQuery for a specific project. However, I seem to be encountering a roadblock when it comes to xmlhttp.onreadystatechange. If a ...

Concealing anchor and span elements using CSS in Internet Explorer 7

I decided to simplify things by creating a style within the document to address my specific issue. The problem I encountered involves a row of 4 links that are designed to resemble buttons. I have chosen to hide the Next link (the 3rd item) using CSS. Whil ...

Error in TypeScript when using Google Maps React with Next.js: there is a possibility that infoWindow.close is undefined

Working on a small project in next.js (typescript) utilizing the google maps api with a KmlLayer. I want my map to interact with another component, SensorInfo. The current setup allows for smooth interaction between them - when SensorInfo is closed, the in ...

What is the procedure for invoking a function when the edit icon is clicked in an Angular application

My current Angular version: Angular CLI: 9.0.0-rc.7 I am currently using ag-grid in my project and I encountered an issue when trying to edit a record. I have a function assigned to the edit icon, but it is giving me an error. Error message: Uncaught Re ...

Ways to parse a json document in vue.js

As a novice in web development, I am currently working with vue-cli3.0 and a local server. <template> <div> </div> </template> <script lang="ts"> import { Component, Prop, Vue } from 'vue-property-decorator&apos ...

The export button in the React Material Table doesn't seem to be displaying correctly

[I am encountering an issue with the React export button in Material Table. The bar is not showing completely. My code includes the following: options = {{exportButton:true}} How can I resolve this?]1 ...

Revamping Sign-out Design in AdminLTE with ASP.NET

For my dashboard, I am utilizing the fantastic AdminLTE. However, I am facing a small styling issue with the logout functionality as it needs to be a form in ASP.NET Core MVC. Does anyone have any suggestions on how to use a normal anchor tag instead of t ...

Set the RegEx so that the entire match is non-capturing

Recently, I've been working with a regex pattern that looks like this: const newRegex = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/; const finalResult = "1988-02-01 12:12:12".match(newRegex); console.log(finalR ...

Is there a parameter I am overlooking when trying to remove an item from a state-stored array using the delete button at line 55?

Need help with the code in the app component. I know it's not ideal, any assistance would be greatly appreciated. I'm a bit lost and can't figure out how to remove an item from the list after it has been added. Everything else seems to work ...

Having trouble incorporating symbols with functionalities using HTML, CSS, and JS

I have implemented a table with three columns: "Action", "Question Subquestion/Text", and "Sort order". Additionally, I have incorporated a feature that allows for drag and drop functionality on the rows. Below is my code snippet: <!DOCTYPE html> &l ...