Only the box that I click on will receive the applied color

I am facing an issue with applying colors to the label colorCheckBox, each with a unique data-id that is derived from the variable colorBoxId, which is globally accessible using

colorBoxId = $(this).closest('tr').data('id');
. The problem arises when I try to add color to a single label upon clicking it; instead of affecting only that label, all labels get colored simultaneously. For further clarification, please refer to the image below: View image here

Upon clicking the label, a div with class "colorList" and id "colorSelectFilter" will be displayed:

<div class="colorList" id="colorSelectFilter" style="display:none; padding: 20px;">
  <a href="#" data-value="1">Green(Approve)<div class="greenBox"></div></a>
  <a href="#" data-value="2">Red(Turn off)<div class="redBox"></div></a>
  <a href="#" data-value="3">Yellow(Check)<div class="yellowBox"></div></a>
</div>

Subsequently, upon selecting a color from colorSelectFilter, the following jQuery function is triggered:

const colors = [null, 'green', 'red', 'yellow'];
$("#colorSelectFilter").find('a').click(function(event){
  event.preventDefault();

  const filter = parseInt($(this).data('value'));
  alert(colorBoxId + "Will be sent to ajax!!");
  var totalColor = colors[filter];
  console.log(colors[filter] + "ErrandSelect");

  if (!isNaN(filter)) {
    // The issue lies here; all labels are affected
    $('.colorCheckBox').css('background-color', colors[filter]); 
  }
});

My objective is to apply the color solely to the label that is being clicked on. Any advice or suggestions on how to achieve this would be greatly appreciated.

Answer №1

The parameter event within the callback function contains a property called target which points to the specific element that was clicked. You can utilize this by passing it through the jQuery function to manipulate it with jQuery, as demonstrated in the example below.

$('.foo').click(event => {
  $(event.target).css('color', 'red');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="foo">Div #1</div>
<div class="foo">Div #2</div>
<div class="foo">Div #3</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

Don't forget to preserve the hover state of divs even after refreshing

I recently added a navigation bar that expands in width upon hovering over it. For an illustration, check out this example: http://jsfiddle.net/Gsj27/822/ However, I encountered an issue when clicking on a button within the hover effect area. After load ...

Having difficulty passing locals in a partial JavaScript file in Rails 3

In my project, I am dealing with multiple JavaScript files such as new.js, index.js, create.js, etc., that handle Ajax calls and other jQuery code. Since these files contain many common code snippets, I have attempted to use partial JS files. For instance, ...

Positioning elements vertically and float them to the left side

I'm struggling to grasp the concept of the float attribute. Here's the code that is causing me confusion: #p1 { border: solid black 3px } <p id="p1" style="float:left">Paragraph 1</p> <a href="https://facebook.com" style="floa ...

Instead of accessing the HTML page directly, you can view the source page by using the F12

I need to extract both data and IPs from VirusTotal.com using selenium and Beautiful Soup in Python. When I make a get request, I only receive the view-source HTML instead of the complete data-rich HTML shown in Inspect mode (F12). Does anyone have any su ...

What is the best way to symbolize a breadcrumb offspring?

In my table representation, the breadcrumb is shown as: <ol class="breadcrumb" data-sly-use.breadcrumb="myModel.js"> <output data-sly-unwrap data-sly-list="${breadcrumb}"> <li itemscope itemtype="http://data-vocabulary.org/ ...

When clicking on an image, it triggers a unique PHP query, however the results obtained are not accurate

Greetings, I have a search feature on my website. When a user inputs a keyword and clicks the search button, it directs them to jobsearch.php which then displays a list of relevant jobs from the MySQL database. The functionality is working perfectly. Howe ...

Issue with React Material UI: Box is not scrollable vertically

I've been grappling with this issue for hours and could really use some assistance. My goal is to create a Chat component that allows vertical scrolling, but I've hit a roadblock. Here's a simplified version of the problem on Codesandbox: ht ...

Bring your text to life with animated movement using HTML5 and/or CSS3

My goal is to have a snippet of text, like "Lorem Ipsum..." slide horizontally into a colored DIV quickly, then smoothly come to a slow stop, continue moving slowly, speed up again, and exit the DIV - all while staying within the boundaries of the DIV. I ...

What is the best way to apply focus to a list element using Javascript?

I recently created code to display a list of elements on my webpage. Additionally, I implemented JavaScript functionality to slice the elements. Initially, my page displays 5 elements and each time a user clicks on the "show more" link, an additional 5 ele ...

Challenges with using $.getJSON in Internet Explorer 8

Unfortunately, I am required to support IE8 and I am struggling to make a simple $.getJSON request work properly. Here is the code snippet: url = "http://www.somejson.com/data.json"; $.getJSON(url, function(data) { var funds = []; var benchmarks = [] ...

What is the best way to align HTML inputs within a grid, whether it be done automatically or manually using JavaScript?

Arrange 20 HTML inputs with various data types into grid-columns as the user inputs data. Ensure that the grid container has div elements correctly positioned for output. ...

Is there a way to show a loading icon during the image change process without using jQuery?

How can I add a loading icon to an image change event without using jQuery? I'm new to the coding world and Stack Overflow community, so please bear with me. I've been searching for a solution to my specific situation but haven't been able ...

Shifting Icon to the Right within the Drawer Navigator Toolbar

While modifying the example code for Material UI's drawer navigator, I decided to enhance it by adding a notification icon and a checkout icon with the Admin Panel typography in the toolbar. However, I encountered an issue where the checkout icon app ...

Utilize Bootstrap to position the button right next to the input field

I am having trouble aligning the button on the right next to the email textbox. Unfortunately, my attempts have not been successful. Fiddler: https://jsfiddle.net/Vimalan/mt30tfpv/4/ <div class="col-xs-3"> <div class="fo ...

Executing an Ajax SPARQL request in Firefox

I've been attempting to run an asynchronous Ajax sparql query on dbpedia using Firefox, but I encountered a strange issue that I can't seem to figure out. Surprisingly, the query works perfectly fine in Chrome, Edge, and Internet Explorer, but wh ...

Utilizing Intel's App-Framework for Dynamic JSON Data Presentation

Can someone provide guidance on how to properly display JSON data retrieved from PHP within an Intel App Framework application? I've attempted using both the .getJSON() and .getJSONP() methods but would appreciate a comprehensive guide on their usage. ...

"Need help on Tumblr: how can I make a div container wrap around a photo

Check out this website. Is there a way to make the div snugly wrap around the image? Currently, it seems to be sticking 4px below the image (as indicated by the red line I added in the background of the div). I'm puzzled as to where those additional ...

What is the purpose of using translateY(-50%) to vertically center an element that is positioned at top: 50%?

I've noticed that this code successfully aligns a div vertically within its parent element: .element { position: relative; top: 50%; transform: translateY(-50%); } My curiosity lies in the reasoning behind this. Initially, I thought that the p ...

What is the process for converting x and y coordinates to align with a fixed display?

When I make an API call, I am receiving X and Y coordinates in the following format: x: "-0.0120956897735595703" y: "0.147876381874084473" To display these coordinates on my minimap images, I have set their display to be absolute. The "left" and "top" pr ...

Is it feasible to utilize the HTML5 video tag in conjunction with a JSON callback?

Curious about the possibility of a video page layout featuring a main screen and smaller clickable screens below it. When clicking on one of the smaller screens, the main screen would display the selected video, similar to YouTube. We have obtained data f ...