Assign a new class to the child element of **this**

Can you please explain how to apply a class to the child elements of this?

I am looking to give a different background color to all Compliant items.

For example:

<div class="tab">
    <tr><td class="compliance">Compliant</td></tr>
    <tr><td class="compliance">Non-compliant</td></tr>
</div>




$(".tab td:contains('Compliant')").each(function () {
    $(this, .compliance).addClass( "success" );
});

Answer №1

Here is a simple solution for your issue:

$(".tab td:contains('Compliant')").addClass("success");

If you are iterating through all the table data cells, you can simply use $(this):

$(this).addClass("success");

As suggested by suresh in the comments, if you need to target specific table data cells, use this approach:

$(".tab td.compliance:contains('Compliant')").addClass("success");

Answer №2

Utilize the hasClass method.

$(".tab td:contains('Compliant')").each(function () {
  if ($(this).hasClass("compliance")){
    $(this).addClass( "success" );
  }
});

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

Presenting charts using flot library

Having Trouble: My x-axis ticks are not displaying correctly on the graph - only the first tick is showing at the far right and no bars are being plotted. I believe I might be overlooking something obvious. Desired Fix Needed: Display all x-axis ticks and ...

What is the best way to showcase a person's first and last name in individual text fields?

Within a drop-down menu, each option is formatted like the following example: scomp23 - Jack Turner By utilizing the code snippet below: var text = $(this).find('option:selected').text(); var split = text.split(' - '); $('#curre ...

Making a post request with Ajax in Node.js

Dealing with an issue in Node js and AJAX POST - the Node request fails to verify if the form data is being handled correctly, consistently triggering the error function. Is there any solution to check the AJAX NODE JS CODE below? $("#submit_btn").click ...

Using touch-action to enable movement from the last item to the first item in HTML/C

Currently, I am utilizing the touch-action property in my carousel which auto slides without any issues. However, I am facing a problem where when I scroll using touch-action, it stops at the last slide instead of looping back to the first slide. My goal i ...

The Angular Material Theme is not being properly displayed on the mtx-datetimepicker component

Issue: While using directives from ng-matero for DateTime with Angular Material for application design, the dateTimepicker element is not applying the angular material theme as desired. https://i.sstatic.net/zPBp3.png Code Example The app.module.ts file i ...

Tips for structuring a SQL query result in an AngularJS application

Forgive me if this code is not up to par with the best standards, as I am still relatively new to angular.js. The issue I am facing is that when the data is returned from my query, it appears as a block of text. Despite using echo statements in search.php ...

The buttons in the Bootstrap modal are unresponsive

I'm attempting to create a modal navigation bar for mobile view but encountering an issue. When I click on the icon bar or menu bar in mobile view, none of the buttons inside the modal are responding. I'm quite stuck on this and would appreciate ...

Position of background image

Attempting to incorporate an image within a textbox in the following format (## representing the image): --------------------- | ## | --------------------- My progress so far: input.loading { background: url(/images/loader.gif) no ...

Validate if the Jquery AJAX response contains any data

I've been attempting to integrate an alert message in my code that triggers if the response is null, but every time I try to do so, something goes wrong. If anyone has any suggestions or assistance with this issue, it would be greatly appreciated. He ...

How can I prevent the content from sliding out of view in a Bootstrap carousel?

I am looking to create a slider that includes a form or text. However, I have noticed that when scaling up by 50-75% or when there is a lot of content, the content extends beyond the slider. Image Check out my code on jsfiddle: jsfiddle.net/sL70r2an/ ...

Exploring the implementation of a many-to-many relationship on a webpage

In the process of creating a library database and dealing with a many-to-many relationship between books and writers, I am contemplating how best to design the user interface for managing this information. When editing a book entry, selecting one or more ...

When using percentages in HTML, the height and width of a background picture may not scale correctly

Currently, I am utilizing Webstorm software purely for HTML and CSS without any additional plugins. In my code, there is a <section> element that only has the <html> and tags as its parent elements. This particular section is assigned an ID th ...

Accessing Jquery's $.get function is currently not possible

I am experiencing difficulty fetching the contents of a json.txt file located in the same directory as this markup. Additionally, there are no errors appearing in Firebug. <!DOCTYPE html> <html> <head> <script type="text/ja ...

Loading images in advance with AJAX for enhanced AJAX performance

My website is structured in a sequential manner, where page1.html leads to page2.html and so on. I am looking to preload some images from the third page onto the second page. After searching, I came across this amazing code snippet: $.ajax({ url ...

Having trouble getting the image to show up in Django from the static directory

In the code snippet below, the image is referenced: Found inside the HTML and CSS files: <button type="submit" class="task-item-button"> <img src="{% static 'x.svg' %}" alt="Close icon"> </b ...

"Create a dynamic HTML newsletter utilizing tables similar to the responsive design principles

Looking to create a mobile-responsive newsletter using my style.css <table cellpadding="0" cellspacing="0" border="0" align="center" width="600" bgcolor="#fff"> <tbody> <tr> <td style="padding-bottom:5px" valign="top"&g ...

The method to extract the followers of an Instagram account using node.js, cheerio, and InstAuto/Puppeteer

Currently, I am attempting to develop a program that generates lists of users who follow specific profiles, and vice versa. Since the Instagram graph API is now inactive, this task has become quite challenging. Despite identifying the correct div element, ...

Displaying image while processing substantial ajax data

I implemented code to toggle the display of a loading image on my web page while making ajax requests. $(document).ajaxStart(function(){ $("#please_wait").css("display","block"); }); $(document).ajaxComplete(function(){ $("#please_wait").css(" ...

Adjust the width of the table to scroll horizontally and size down to fit within the

My webpage is structured with a sidebar and content section, using flex display with the sidebar set to 0.15 flex and the content set to 0.85 flex. I want this page to be full width of the viewport. The issue arises when I try to incorporate a table into ...

Webmatrix - Retaining query parameters throughout the website's pages

After successfully implementing pagination on my search page, I ran into an issue where the query strings used to build the search query are lost when transitioning between pages. Is there a simpler way to retain these query strings, or will I have to delv ...