My attempt to apply the toggled classList styles has been unsuccessful in changing the appearance of the element

I have been experimenting with classLists.toggle but for some reason, I am unable to see the applied styles on the element. I am struggling to pinpoint where exactly I am making a mistake. I have set up IDs without any current styles so that a class can be toggled on or off upon clicking, but I can't understand why the styles are not being applied. The toggled style is being triggered but not its associated styles, which is quite strange. Can someone shed light on why this might be happening?

const clicked = () => {
  document.getElementById("box").classList.toggle("toggle");
  document.getElementById("text").classList.toggle("change");
}
body {
  background-color: #42b883;
}

#text {
  position: absolute;
  font-size: 70px;
  right: 750px;
}

#box {
  height: 100px;
  width: 100px;
  background-color: blue;
  cursor: pointer;
}

.toggle #box {
  background-color: black;
}
<div id="box" onClick="clicked()"></div>
<h2 id="text">Test</h2>

Answer №1

It appears that your CSS rule is incorrect.

.toggle #box {

The correct format should be:

#box.toggle {

Your code expects the following structure:

<div class="toggle">
  <div id="box"></div>
</div>

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

Issue with NodeJS Express's reverse proxy due to an invalid TLS certificate alternative name

I have configured a reverse proxy on my endpoint as shown below: var express = require('express'); var app = express(); var httpProxy = require('http-proxy'); var apiProxy = httpProxy.createProxyServer(); var serverOne = 'https://i ...

Opening a PHP file without performing thorough validation in JavaScript can lead to security vulnerabilities

I am in need of some assistance. I am currently working on a form and seem to be encountering an issue. Whenever I click on the submit button without checking the full validation, it performs the PHP action. For instance, when I input a serial number into ...

Ways to fix text overlap in a pill

Click here to view the code on jsBin I quickly copied HTML/CSS code to jsBin using a tool called SnappySnippet. I attempted various solutions, but none of them worked. The best option for me seems to be overflow: ellipses word-break: break-word; word-b ...

Integrating Livefyre npm with Meteor

Currently, I am in the process of creating a custom package to integrate the livefyre npm module into Meteor after receiving a request from a client. Despite following the instructions provided here, I keep encountering errors that state Errors while scann ...

Trouble arises when trying to transfer the selected "value" from a dropdown in a form to a PHP variable using JavaScript

I am attempting to transfer the selected option value from a HTML form to a PHP variable using Javascript <script type="text/javascript> function changeDir(ID){ var dir = document.getElementById(ID).value; //For debugging purposes, I have in ...

Tips for changing a list item by pressing the Enter key

I am looking to enable the editing of an ordered list by using contenteditable. As the user makes changes or adds new elements to the list, I want to be able to manipulate the text (such as wrapping it in a span tag or replacing the text). Currently, I ha ...

The issue with Ng-Zorro select preventing the display of selected items when using NgModel

I am currently working with Ng-Zorro's multiple select feature, housed in a drawer. While populating the select element with a list of available options and pre-selected items upon opening the drawer, I've encountered an issue where the already c ...

Unable to access information between the main domain and subdomain

Currently, I have an Ajax request set up on a domain called xyz.com. This request is being used to pull data from abc.xyz.com via another Ajax request. var request = $.ajax({ type: "POST", url: "https://abc.mno.com/vending/mainvending.php" ...

What is the reason behind TypeScript indicating that `'string' cannot be assigned to the type 'RequestMode'`?

I'm attempting to utilize the Fetch API in TypeScript, but I keep encountering an issue The error message reads: Type 'string' is not assignable to type 'RequestMode'. Below is the code snippet causing the problem export class ...

jQuery: Eliminating Parent Elements

Currently, I am developing a small web application that allows users to create lists which are then formatted and emailed to them. However, I am facing difficulties in implementing a method that will allow users to delete list items without clearing the en ...

Tips for addressing lag problems in my Three.js game as time progresses

My game in Three.js doesn't start off with any lag, but after a few minutes, the performance begins to slow down on computers running it. I've tried reviewing my code and checking my arrays, adjusting values to troubleshoot, but so far, nothing s ...

What are some ways to direct users from one page to another without relying on server-side programming?

Is there a way to create a redirect page using jQuery or JavaScript? What is the process of writing client-side scripting code to redirect the browser from one page (page1) to another page (page n)? ...

Ways to modify HTML tag content using Java

Is there a way to update HTML content within a tag using Java? Take the following example: Before: <html> <head> </head> <body> <div>text<div>**text**</div>text</div> </body> & ...

What are some creative ways to design my dropdown menu?

I recently completed a tutorial on creating my own custom drop down menu. However, I am struggling to style the submenus. I can't seem to locate the correct class to modify it. My goal is to have the sub menus display in a single line and within a box ...

Tips for preventing CSS grid from expanding rows excessively

The following code may seem a bit messy, but it demonstrates what I'm trying to achieve: having the nav element adjust its height based on its content, while the aside element extends down along with the main content. However, if I remove the two som ...

Enable the ability for anchor links to be clickable when the CSS media print format is used to generate

To illustrate: <a href="https://www.google.com">Google anchor link</a> on the website, it displays as: Google anchor link When printed, it shows as: Google anchor link(https://www.google.com) To address this issue, I included in the CSS: ...

Ember employing the [needs] declaration to establish communication between views

Is it possible to interact between emberViews? I had a setup using controllers that worked well before. >> Index Controller var StudyController = Ember.ArrayController.extend({ needs: ['study/study'], actions: { fi ...

The complete guide to effectively concealing double arrows within a Bootstrap 4 custom-select component

If you're looking to hide the double arrow on the right of the bootstrap 4 custom-select field, there have been solutions provided here. However, even after implementing those changes, the text on the right-hand side still gets obscured by the opaque ...

What are some creative ways to incorporate text into a slider design?

I have a box with 3 links and a fontawesome icon in my code snippet. I want to be able to click on the font icon and make the content slide. Initially, only three links should be shown, but when I click on the arrow, it should display the other icons. When ...

Vue.js mobile app may show a loaded DOM that remains invisible until the screen is tapped

I am facing a peculiar issue that has me stumped. On my mobile device, my page initially loads like this homepage However, once I tap the screen, all the components suddenly appear. Is there a way to simulate a click on my mobile? I'm struggling to u ...