Is there a way to validate td content without considering spaces or new lines?

I am working with a table that has dynamic values in its td cells. Some td cells will be empty while others will have values. I want the empty td cells to have a red background, and the filled td cells to have a green background. I attempted to achieve this using CSS with the following code:

td:empty {
          background-color: red;
        }

However, I encountered an issue where the empty td cells contain only spaces and lines, rendering the td:empty selector ineffective.

In other words, the CSS interprets these spaces and lines as content within the td tag, even though they are essentially fake content. How can I resolve this issue?

--- Update --- Here is the output result: https://i.sstatic.net/b1wd5.png And here is the script:

<td>
    {% for mission in missions|get_missions_by_user:user %}
        {% if mission.start_date|date:"d-m-Y" == week_dates.6.date|date:"d-m-Y" %}
            {{ mission.title }}
             <!-- Filled cell - this should have a green background -->
            <br>
        {% endif %}
        <!-- Empty cell - this should have a red background -->
    {% endfor %}
</td>

Answer №1

Have you considered using the yesno function to add the necessary classes?

CSS:

td.empty {
  background-color: blue;
}
td.filled {
  background-color: yellow;
}

HTML:

<td class='{{missions.count|yesno:"filled,empty"}}'>
    {% for mission in missions %}
        {% if mission == 'MISSION' %}
            {{ mission }}
            <!-- filled cell - displayed with yellow background -->
            <br>
        {% endif %}
        <!-- empty cell - displayed with blue background -->
    {% endfor %}
</td>

Disclaimer: I have limited experience with django and python, so my approach may not be the most efficient. In javascript, I typically use missions.length. I hope this suggestion proves helpful.:)

Answer №2

Big thanks to everyone who pitched in to help me out. After days of hunting and consulting with JavaScript experts, I finally came across this amazing script:

This script provides an efficient alternative to targeting empty table cells by also taking care of trimming spaces and lines:

Behold the JavaScript magic:

    $("#tableID td:not(:first-child)").each(function () {
        if ($(this).text().replace(/\s/g, "").length > 0) {
            $(this).css("backgroundColor", "green");

        } else {
            $(this).css("backgroundColor", "red");


        }
    });

Answer №3

To change the appearance based on whether a table cell is empty or not, you will need to add and remove classes dynamically. When a table cell is empty, you can add a specific class that applies a background style to it.

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

Is it possible to locate an element using regex in Python while using Selenium?

Is it possible to interact with a dynamically generated dropdown list using Selenium if I only know the phrase present in its id or class name? Can Selenium locate an element using regex and click on it accordingly? ...

The CSS media query isn't functioning properly on Safari browser

Currently, I am working on a project using React JS and CSS. In order to make it responsive, I have implemented media queries. However, I have encountered an issue where the media query is not functioning properly in Safari browsers on both phones and PC ...

Tips for launching a Django application on a hosting provider

As someone new to the world of web development, I recently purchased a start-up package from Siteground to host my graphic design portfolio website. The site itself is a simple Django app featuring a one-page portfolio and a contact form that utilizes Djan ...

I am encountering a 'Cannot GET /index.html' error when running my Node.js and Express application, even though I do not have an index.html file

I'm puzzled by the error I'm encountering. Everything runs smoothly on my local machine, but when I try running it on Linux, this error pops up. I've already created ejs files and included all necessary components. Additionally, there is no ...

Tips for designing an Ionic app with a Pinterest-inspired layout

Recently stepping into the world of Ionic development, I find myself facing a challenge in creating a Pinterest-inspired layout using Ionic. Specifically, I am struggling to achieve a two-wide list of items with varying heights. Can anyone offer guidance ...

Tips on ensuring a <video> element occupies the full height and width of the screen

I am currently working on an Angular 6 project where I am live streaming a user's webcam to an HTML element. My goal is to simulate the experience of capturing a video or photo on a mobile device. However, the size of the video is currently small. I ...

Generating a dynamic list by utilizing database data once a button has been clicked

I'm looking to create a feature on my website where clicking on a button will populate a paragraph below it with data retrieved from a database query containing two columns. The first column provides the text for a list, while the second column contai ...

My attempt to use the Redux method for displaying data in my testing environment has not yielded

I've been attempting to showcase the data in a Redux-friendly manner. When the advanced sports search button is clicked, a drawer opens up that should display historical data when the search attributes are selected. For this purpose, I implemented the ...

Issue with jQuery UI: Accordion not functioning properly

I have created a nested sortable accordion, but there seems to be an issue. Within the 'accordion2' id, the heights of each item are too small and a vertical scroll bar appears. The content of the item labeled '1' is being cut off, show ...

The tab content refuses to show up in its designated fixed location

I've been working on creating a responsive tab system that functions as an accordion on smaller mobile devices, inspired by this example. I've made great progress and I'm almost where I want to be, but for some reason, the active tab content ...

Is there a way to remove the initial number entered on a calculator's display in order to prevent the second number from being added onto the first one?

I am currently in the process of developing a calculator using HTML, CSS, and JavaScript. However, I have encountered an issue with my code. After a user inputs a number and then clicks on an operator, the operator remains highlighted until the user inputs ...

Tips for ensuring an animation is triggered only after Angular has fully initialized

Within this demonstration, the use of the dashOffset property initiates the animation for the dash-offset. For instance, upon entering a new percentage in the input field, the animation is activated. The code responsible for updating the dashOffset state ...

Retrieve image file from computer's hard drive

I have the following HTML Tags: <input id="bigPicture" name="bigPicture" type="file" value=""> <img src="test.png" id="test1"> User select file and I want to preview this file inside ...

Struggling to Fetch Data from REST API with ng-repeat in Angular JS

After making a REST call from Angular JS and receiving a response from the API, I am looking to display messages on the front end using ng-repeat in my controller. $http({ url: "http://localhost:8080/services/AddConfig", method: "POS ...

A user-friendly JavaScript framework focused on manipulating the DOM using a module approach and aiming to mirror the ease of use found in jQuery

This is simply a training exercise. I have created a script for solving this using the resource (function(window) { function smp(selector) { return new smpObj(selector); } function smpObj(selector) { this.length = 0; i ...

Exploring the Live Search Functionality on an HTML Webpage

I am attempting to create a live search on dive elements within an HTML document. var val; $(document).ready(function() { $('#search').keyup(function() { var value = document.getElementById('search').value; val = $.trim(val ...

Automatically trigger a page reload using a jQuery function

Currently facing an issue with jQuery. I created a drop-down menu and would like it to expand when the li tag within the menu is clicked. Even though I am using the jQuery click function successfully, there seems to be a problem where the webpage refreshe ...

New to CSS/Ruby - What causes two adjacent buttons to have varying sizes?

The varying sizes of my search and login buttons located beside each other are puzzling me. Could it be due to the fact that the search button is enclosed within a submit_tag argument while the login button is not? If this is indeed the case, how can I mak ...

refreshing the webpage with information from an API request

I am completely new to web development, so please bear with me. I am attempting to make a simple API call: const getWorldTotal = async () => { const response = await fetch('https://cors-anywhere.herokuapp.com/https://health-api.com/api/v1/cov ...

Equivalent of ResolveUrl for loading scripts without the need for server technology

I am in the process of converting an ASP.NET web page into a plain HTML web page. Most of the work is done, however, I am having trouble replacing the ASP.NET Page.ResolveUrl function when setting a reference to a javascript file: <script src="<%= ...