CSS Selector for table cell containing a checked input in the cell before it

Is it possible to select a table cell based on the state of a previous cell using CSS? I have a feeling that this may not be achievable with CSS alone. Can someone confirm my suspicion?

Let's consider this example:

<table>
    <tr>
        <td>Row with checked input</td>
        <td><input type=checkbox checked /></td>
        <td>should show.</td>
    </tr>
    <tr>
        <td>Row with unchecked input</td>
        <td><input type=checkbox /></td>
        <td>should be hidden.</td>
    </tr>
</table>

Here is the CSS code that I attempted:

td:last-child { display:none; }
input:checked ~ td:last-child { display:initial; }

The first part of the selector can hide unchecked inputs, but the second part fails to make the input visible again. It seems like the ~ combinator struggles with targeting children of previous siblings. Is there a workaround in CSS for this issue, or should I consider a JavaScript solution?

Feel free to check out this JSFiddle link for live testing and experimentation.

Answer №1

Unfortunately, the current HTML structure does not allow CSS to target parent elements. For example, a pseudo-selector for an input cannot directly affect its parent td, and similarly, it cannot affect its parent's sibling.

If you are open to adjusting your HTML layout, one solution is to place the element that needs to be hidden or shown as a direct sibling inside the same table cell.

<table>
<tr>
    <td>Row with checked input</td>
    <td><input type=checkbox checked /><span>should show.</span></td>
</tr>
<tr>
    <td>Row with unchecked input</td>
    <td><input type=checkbox /><span>should be hidden.</span></td>
</tr>
</table>

CSS

input:not(:checked) + span { display:none; }

You can view this implementation on JSFiddle here.

Answer №2

As of now, accomplishing this task with CSS alone while maintaining full browser support remains a challenge. To ensure cross-browser compatibility, you may need to implement an event listener/handler using JavaScript. Utilizing a jQuery solution could be the most straightforward approach if you are already familiar with it.

For example, you can use the jQuery(":checked") selector, and refer to the API documentation here.

Creating simple functions for .hide() and .show() can help achieve your desired outcome, as outlined in the API documentation.

It's worth noting that the :checked selector is currently only supported by Opera, as indicated by W3schools.com

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

Using different browsers can cause web fonts to appear rough and pixelated

While exploring the issue of "jagged" or "pixelated" fonts, I made an interesting discovery. After creating a website locally and viewing it in Firefox, the font from Google Webfonts appeared flawless - almost like it was straight out of Photoshop. It wasn ...

By default, the D3 hierarchy will collapse to the first level

I've been working on code to create an indented tree structure. My goal is to initially collapse the tree and show only the first level children or the root node. I tried a few different approaches, but none were successful. The current portion I have ...

Updating Select Options with Multiple Values using Javascript

My goal is to update the selected value of multiple select elements simultaneously using JavaScript. However, I am facing an issue where my script only updates one select element instead of all of them on the page. Unfortunately, I cannot modify the id att ...

Overcoming Troublesome Login Input Box in Web

In an effort to streamline tasks in our office, I am working on automating certain processes. Specifically, this program is designed to log into our insurance company's website and extract information for payroll purposes. Below is the code snippet th ...

Issue encountered while attempting to include a background image in React JS

I encountered an issue when attempting to include a background image in react js. ./src/index.css (./node_modules/css-loader/dist/cjs.js??ref--5-oneOf-4-1!./node_modules/postcss-loader/src??postcss!./src/index.css) Unable to locate module: An attem ...

What methods are available for populating bootstrap columns dynamically with the use of jquery, php and smarty templating?

I am currently working on developing a dynamic news article module that needs to resemble the layout of a traditional newspaper article. The height of the news article must be restricted based on the size of the current view port. All information for the a ...

Manipulating child classes using jQuery

I am struggling to display an X icon next to a tab on my page when the tab is clicked, but I am facing difficulties in toggling its visibility. The issue arises when trying to access the span element within the tabs. $('.tabs .tab-links a').on(& ...

Center an image and superimpose text in the middle of the webpage

I am attempting to center an image with text overlay in the middle of a page. Although the image is centered correctly, the overlay text remains off-center. Below is the code snippet. The text should be positioned in the top left corner of the image. &l ...

Troubles with Bootstrap's navbar dropdown menu functionality

I seem to be encountering a problem with the dropdown list in my navigation bar. It was functioning correctly yesterday, but something seems to have gone awry as Bootstrap is no longer working for the dropdowns only. I'm puzzled as to why this sudden ...

I want to know how to move data (variables) between different HTML pages. I am currently implementing this using HTML and the Django framework

I am currently working on a code where I am fetching elements from a database and displaying them using a loop. When the user clicks on the buy button, I need to pass the specific product ID to another page. How can I retrieve the product ID and successful ...

Ways to move to the next line following the navigation bar text

Below is the code snippet that I am working with: <nav class="navbar navbar-expand-sm navbar-dark "> <div class="container-fluid"> <a class="navbar-brand" href="#">LOREM IPSUM</a&g ...

What is the best way to use jQuery to apply CSS only to elements with a specific attribute set?

Currently, I am attempting to utilize jQuery to apply specific CSS styles to elements, but only those that possess a particular attribute. The rationale behind not resorting solely to CSS is due to the immense amount of content on the site, making it impr ...

Conceal and reveal content using jQuery

I'm struggling with creating a functionality to hide and show content when a user clicks on a button (checkbox). I've written some code but it's not working as expected. The issue is that the content doesn't hide when the checkbox is cl ...

How can you ensure a cohesive visual style throughout various Razor and HTML views?

In my ASP.Net MVC 5 application, I have a Layout.cshtml and HTML view page included. However, I want to maintain a consistent look and feel across multiple views in the application. I am aware that this can be achieved by using a Razor view page with the f ...

Troubleshooting problems with dataTransfer and the file input type in JavaScript

Working on a project, I'm currently exploring the idea of implementing a JavaScript drag-and-drop feature. Typically, when using drag and drop, one would pass the event.dataTransfer.files to an ajax call for uploading the file to an external PHP file ...

An issue occurred with the DOMException while trying to execute the 'setAttribute' function on the 'Element': '{{' is an invalid attribute identifier

Currently, the code is under development and some methods are still empty while the *ngIf directives may not be correct in terms of logic. However, even with these issues, I encountered the same error without bothering to remove them at this stage. While ...

Unable to use the ordered tag correctly in typing

Please review the HTML and CSS Code provided below: HTML : <ol> <li>Test</li> <li> <ol> <li>Sub-Chapter</li> <li>Sub-Chapter</li> </ol> < ...

Tips for configuring a file using javascript and angular.js

I am in need of some assistance. I would like to create a configuration file that stores all the paths for JavaScript files, which can then be dynamically called and included in the index page using Angular.js or JavaScript. The structure of my files is ex ...

What steps can be taken to ensure the visibility and accessibility of two vertically stacked/overlapped Html input elements in HTML?

I have encountered a challenge with my HTML input elements. There are two input elements that overlap each other, and I would like to make both inputs visible while only allowing the top input to be editable. I have tried several approaches involving z-ind ...

Display PHP Array data in a table on a webpage

I need help arranging the results from a MYSQL database into an HTML table. Right now, each item is displayed in one column per row. Here is my current code: <?php $catagory=$_GET["q"]; $con = mysql_connect("localhost","cl49-XXX","XXX"); if (!$con) ...