Avoiding using ID selectors when working with a checkbox hack

I've shared about my project previously. Twice, actually. While the responses have given me better insight into my situation, they haven't been directly applicable. I take responsibility for this as I was posting an incomplete version of the final code that didn't fully explain my requirements.

Essentially: I need to incorporate a simple checkbox workaround into a CMS, but the CMS removes id selectors. As a result, code that should resemble this:

<input type="checkbox" name="thisisaname" id="thisisanid"><label class="thisisanid" for="thisisanid">Type 1</label>

...looks like this:

<input type="checkbox" name="thisisaname"><label class="thisisanid" for="thisisanid">Type 1</label>

This naturally causes issues, and it seems impossible to make any adjustments. The CMS doesn't provide a CMS-specific alternative like ClientID=. Both jQuery and JavaScript are unavailable to me as the CMS removes them.

It's a situation where a seemingly simple task is complicated by a CMS to the point where I'm unsure if it's even achievable. I'm not a CSS expert. I only know what I need for small projects for friends. I apologize for bringing this up again, but the uncertainty is driving me mad.

This is the code. It's not polished and not fully styled, but it's a closer representation than what I've shared before:

#basesurround {
  background: #000000cc;
  margin: 0 auto;
  width: 75%;
}

.information-wrap { display:flex; vertical-align:top;} 
.information-wrap aside { background: #00000066; vertical-align: top; flex: 1 1 250px; min-width: 150px; padding: 0; max-width: 200px; }
...

I'm stuck. My CSS knowledge is limited, and this mandatory CMS makes even simple tasks extremely difficult. Here's what I've tried:

  • Using nth-child / nth-of-type selectors: Suggested by Temani Afif. A great solution, but the structure may change frequently for this code's use.

  • Placing the input above the labels: Recommended to me, but coding with the input above the label is unfamiliar territory.

  • Exploring [attribute|=value] selectors as an alternative to id: Tried this, but can't get it to function. Unsure if it's due to the code or my skills.

So... Is there a specific method I've missed in my research to overcome the id= limitations?

For what it's worth, the goal here is to create a flexible code where the INFO CARD section can be easily duplicated for sorting movies/books/other info, while allowing simple changes to the figure class=.

Answer №1

The issue at hand pertains to the limited functionality of the for attribute within a <label> element - it specifically looks for a form control with a matching id attribute. If no matching id is found, the <label> remains unassociated with any form control, rendering the checkbox hack ineffective.

Given that we cannot initially assign an id to the checkbox in the document's HTML markup, a viable solution is to dynamically assign the id using the Document Object Model (DOM). This can be achieved in a single line of code:

document.querySelector('[name="thisisaname"]').id = 'thisisanid';

Refer to the Working Example showcased below, highlighting the absence of the id in the initial markup:

document.querySelector('[name="thisisaname"]').id = 'thisisanid';
label {
cursor: pointer;
}

label:hover {
font-weight: bold;
text-decoration: underline;
}

input[type=checkbox],
input[type=checkbox] + p {
opacity: 0;
}

input[type=checkbox] + p {
transition: opacity 1s linear;
}

input[type=checkbox]:checked + p {
opacity: 1;
}
<label class="thisisanid" for="thisisanid">Click Me</label>
<input type="checkbox" name="thisisaname">
<p>Checkbox hack is working</p>

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 extract the text enclosed in <li> tags using JavaScript?

Can someone help me with retrieving the text content of li tags? I managed to get a list of lis, but I'm having trouble assigning the text content to my country object. Snippet var lis = document.getElementById("navbar").getElementsByTagName("l ...

Mouseover function ceases to function once an element is dropped

After referencing this discussion, I successfully managed to drag items from one scroll overflow to another. However, the issue arises when these items (or their clones) are in the other scroll overflow as they do not respond to .mouseover(). The goal is ...

Is it possible to nest a parsys within another parsys in this context?

On my webpage, I have a paragraph system (parsys) with a component that contains two inner paragraph systems. When I try to add a rich text component to each of these inner paragraph systems, the components overlap and become uneditable. Is it possible t ...

What is the best method for removing a class with JavaScript?

I have a situation where I need to remove the "inactive" class from a div when it is clicked. I have tried various solutions, but none seem to work. Below is an example of my HTML code with multiple divs: <ul class="job-tile"> <li><div ...

Steps for deploying an ejs website with winscp

I have developed a Node.js web application using ExpressJS, with the main file being app.js. Now I need to publish this website on a domain using WinSCP. However, WinSCP requires an index.html file as the starting point for publishing the site. Is there ...

Tips for creating an inline style with "border: 1px solid #72777d"

For the boxes I applied borders using "border: 1px solid #f2f2f2" as depicted in the image. The issue is that with 1px solid borders, they overlap each other. Is there a way to use inline borders with CSS instead? Thank you in advance ^_^. border: 1px s ...

What is causing my server to mysteriously alter the style of index.html?

After setting up a node, express, react app, I realized that when express serves static content like my CSS file, the source of the index.html shows an additional style tag in the head section: <style type="text/css">* {}</style> I confirme ...

if the checkbox is selected, navigate to the displayed section

In my scenario, I have a total of 5 checkboxes and corresponding to each checkbox, there are 5 div elements. The functionality that I need is that when a checkbox is checked, the associated div should be displayed; otherwise, it should be hidden. The check ...

Steps for Creating an Animation Tool based on the Prototype:

One question that recently came up was: What is the best approach to tackling this issue? Developing a tool that enables designers to customize animations. To make this process easier, it is essential to create an AnimationSequence using JavaScript that ca ...

Firefox is unable to render the jQuery file from ajax.googleapis.com

I have Mozilla Firefox version 20.0.1 installed, and I am using the following code on my website: <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <!--<script s ...

Expanding a non-bootstrap navigation bar upon clicking the menu

I'm having some difficulty getting my navigation menu to show up when I click the navigation menu icon. HTML nav role="navigation" class="navbar"> <div class="nav-header"> <a href="#"><span style="font-family: 'Cab ...

Troubleshooting issue: Angular not resolving controller dependency in nested route when used with requirejs

When the routes are multiple levels, such as http://www.example.com/profile/view, the RequireJS is failing to resolve dependencies properly. However, if the route is just http://www.example.com/view, the controller dependency is resolved correctly. Below ...

Tips for customizing the background of form inputs in React-Bootstrap

Currently, I have a next.js project with react-bootstrap. I am attempting to change the default blueish gray background color (#e8f0fe or rgb(232, 240, 254)) that bootstrap uses for form inputs to white. To achieve this, I am customizing the bootstrap var ...

Expanding CSS transition for child elements as they increase in size

I am currently working with Vuetify and have encountered an issue with a v-list-item that contains a title and a subtitle. The problem is that the title is restricted to displaying only one line. If the title exceeds this limit, it gets cut off and shows " ...

angular bootstrap dropdown opening on the left side

I am facing an issue with the angular bootstrap dropdown. I have successfully implemented it, as shown in this link. However, the problem is that the dropdown opens on the right side by default. Is there a way to make it open on the left side instead? Bel ...

Items vanish when hovering over them

Creating nested links in the sidebar navigation bar with CSS has been a challenge for me. While hovering over main links, I can see the sub-links being displayed. However, when I try to hover/click on the children of the sub-links, they disappear. Note: M ...

Angular UI grid: Arranging numbers in a straight line at the decimal point

I am interested in aligning decimal numbers in Angular UI Grid as shown below. 11.293 .89 233424 .34345 I have considered different approaches such as using a cell template with aligned divs or transparent 0s. Has anyone successfully imp ...

Issue with Safari not displaying properly when using float:left and float:right (works fine in Firefox and Chrome)

My website has a left side column and a right side column, which display correctly in Firefox and Chrome. However, I am experiencing issues with Safari. Any insights on what could be causing this problem and why it only occurs in Safari? View the code on ...

Importing an Excel file in .xlsx format cannot be done through AJAX JQUERY

I am trying to display the content of an Excel file when a button is clicked. It works fine when I change the extension to .csv, but when I change it to .xlsx, I get a character type error that looks like "y!+EfMykK5=|t G)s墙UtB)". These strange charact ...

Deliver real-time notifications to linked users by leveraging socket.io and node.js

Is there a solution for sending real-time updates to connected clients every second without using the setInterval() function in nodejs and socket.io? Please provide an alternative method that fits my specific scenario. ...