Having trouble getting the CSS checkbox:checked selector to work properly?

My attempt at changing the color of some text within a table based on checkbox selection doesn't seem to be working. The :checked attribute isn't having any effect. Here's the code snippet:

<html>
<head>
<style type="text/css">
.methodoption
{

}
#row1
{
    color: blue;
}
#buyer1 + #row1
{
    color: blue;
}

#buyer1 input[type=checkbox]:checked ~ #row1
{
    color: red;
}

</style>
</head>
<body>
<div align="justify" style="font-size: 23px;"> 
                                Social compliance has experienced significant changes in recent years. Due to industry disasters and consumer awareness, stakeholders are reevaluating their approach to compliance. At WRAP, we strive to adapt our programs to meet these evolving needs.</div><br/>
<div align="Center" style="Font-size: 29px;">WE TALKED TO <STRONG>50</STRONG> BUYERS, THOUGHT LEADERS,<br/>AND OTHER STAKEHOLDERS IN THE SOCIAL COMPLIANCE ARENA</div>
<div align="Center"><img src="infogfx.png" height="350" width="900"></div><br/><br/>
<table align="center" width="600" style="background-color: #aaaaaa;"><tr><td align="center"><em><strong style="font-size: 25px;">WHO ARE THEY?</strong></em></td></td>
<tr><td>In exchange for their participation in they survey, we have agreed to keep the identities of those who particiapted anonymous.</td></tr><table>

<form action="" method="post">
<table>
<tr><td><input type="checkbox" name="buyer1" id="buyer1"></td><td>1</td><td class="methodoption"><span id="row1">Stakeholder Expectations</span></td></tr>
<tr><td><input type="checkbox" name="buyer2a" id="buyer2a"></td><td>2(tie)</td><td class="methodoption">Factory Compliance/Strengthen Supply Chain</td></tr>
<tr><td><input type="checkbox" name="buyer2b" id="buyer2b"></td><td>2(tie)</td><td class="methodoption">Corporate Philosophy</td></tr>
<tr><td><input type="checkbox" name="buyer4" id="buyer4"></td><td>4</td><td class="methodoption">Integrity of Brand</td></tr>
<tr><td><input type="checkbox" name="buyer5a" id="buyer5a"></td><td>5(tie)</td><td class="methodoption">Risk Management</td></tr>
<tr><td><input type="checkbox" name="buyer5b" id="buyer5b"></td><td>5(tie)</td><td class="methodoption">Right Thing To Do</td></tr>
</table></form>

</body>
</html>

Answer №1

The concept of the General Sibling Selector ~ is to target siblings, which are elements that share the same parent. In your case, the input element and the #row1 element are not in the same parent td, making it unselectable.

To make this work, you would need to include both the #row1 element and the input element within the same td:

<td>
    <input type="checkbox" ... />
    <span id="row1"> ... </span>
</td>

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

How can I utilize a PHP variable as the height and width parameters in a CSS style?

After spending a considerable amount of time on this project, I am still struggling to make it work. Can someone please guide me on how to correctly assign the width and height values to the img element using variables? for ($x = 1; $x <= $aantal; $x+ ...

Leverage the power of HTML5 localstorage in order to maintain the toggle state of jQuery even

After discovering the HTML5 "localstorage" function, I decided to try implementing it in combination with jQuery toggle to keep a div in its most recent state even when the page is refreshed. It seems like a more modern alternative to using cookies. Howeve ...

Tips for successfully passing array index as an image source in Vuejs with v-img?

Hi there! I'm currently attempting to cycle through an array of URLs and insert them into the src attribute. I'm struggling with the correct syntax to accomplish this task. Would you be able to lend a hand? I have an array named DataArray that co ...

The hyperlink is pointing to the local host instead of the intended website

I'm having an issue with a table that has various columns. One of the columns is for websites, and when clicked on, it should redirect me to the corresponding website. However, it seems to be referencing my localhost instead. It should actually be: ...

Creating a seamless transition from 2 CSS grid columns to 1 on mobile devices without the need for a media query

Is utilizing a media query the sole solution for keeping itemX and itemY in the same cell on each device? Are there any native CSS grid methods that could achieve the same result? Feel free to check out the demonstration on the following fiddle: https://j ...

Tips for applying CSS to background images

Just working with one div here <div id="particles-js" > and in my CSS, I've got this: #particles-js{ width: 100%; height: 100%; background-color: #b61924; background-image: url("../js/image.jpg"); background-size: cover; backg ...

Is there a way for me to identify what deletes CSS classes from an element during the first page load?

On a page where an element (specifically, a TextBox) initially has two CSS classes assigned when it loads, something strange is happening. After the page renders and JavaScript runs, the class attribute of the input element mysteriously becomes empty. I c ...

Arrangement of Divs Using CSS and HTML

Working on a New Website Hey there, I could really use some assistance in recreating the design shown in this image for my website project. I've made some progress but it's not quite coming together as expected. I'm struggling with positio ...

Button remains behind the image, not moving to the front

I have placed my button on the image, but I want it to sit on top of the image. However, the z-index: 1; property is not working as expected. I have tried all possible position attributes to see if it works, but there was no change. I also added z-index to ...

Is there a way to eliminate the additional and varying pseudo-padding on text inputs in both Webkit and Gecko browsers?

The issue I'm facing is specific to input[type="text"] elements in Webkit. No matter what adjustments I make, it seems like there is an additional padding: 1px 0 0 1px (top and left only) applied to the element. A similar anomaly occurs in Gecko as w ...

Creating a scoreboard layout with HTML and CSS

I am attempting to create a scoreboard in the following format: 0 - 0 Home - Away The issue I am facing is that if the length of the Home team's name is longer, it pushes the dash (-) further. I want the dash to be a ...

Basic website layout

As someone who is new to web programming, I kindly ask for your patience :) I have a goal to design a page similar to the one linked below https://i.sstatic.net/HbJF3.png There are two key functionalities I am aiming for: 1) Clicking the button on the r ...

Issue with animating multi-column layout in Chrome causing display problems

I'm currently utilizing jQuery to create an animated multi-column layout that extends off the screen horizontally. I have implemented two controllers - one red and one blue - to navigate back and forth within the layout. Strangely enough, while the an ...

Issue with changing the opacity of a Javascript button style is not functioning correctly

The button's opacity used to change gradually in increments of 0.1 every 500 milliseconds, but now it instantly changes to 0.9 without the delay. Issue: Despite being placed within a window load handler and all elements loading properly, the loop is ...

Troublesome Navigation Bar Dilemma: A Conundrum in HTML and CSS

Help needed with my homepage navigation bar that is acting up. The last button link seems to be extending further down than it should, and it's turning the text below it into a hyperlink as well. I'm not very experienced with programming, so any ...

Achieving full width for the elements in a row with Flexbox: A step-by-step guide

I am dealing with a flexbox grid that contains random width images: https://i.stack.imgur.com/pfPeU.jpg I want to stretch the elements in each row to full width to eliminate any white space - Is there a way to achieve this using CSS? justify-content do ...

Ensuring the correct range with HTML TextBoxFor

Is there a way to validate user input in a TextBoxFor to ensure it is less than a certain number at run-time? Here is the current code snippet for reference - <div class="col-md-3 input-group"> <span class="input-group-addon ...

In IE9/10, the absolutely positioned pseudo element within a table cell does not fully overlay its parent

My layout includes nested div elements displayed as a table and table-cell, with each cell containing an absolutely positioned :before element that covers the entire cell. While this setup works perfectly in most browsers, I am facing an issue in IE9, 10, ...

What is the proper way to showcase an element positioned absolutely within a Dialog window using material-ui?

My current project involves using React, TypeScript, StyledComponent, and MaterialUI. I am looking to add a button to a Dialog window (material-ui) similar to the design shown in this example image. However, when I apply position: absolute to the button, ...

The CSS properties of a div element include the :before pseudo-element with a "shape" style that does

Having trouble finding a way to neatly contain this circle inside the div without any overflow. Looking for an elegant solution! Example of desired outcome .wrapper{ background: #efefef; height: 250px; } .wrapper::before{ width: 300px; height: 300 ...