Is it achievable to use multi-level :not selectors in CSS / JS querySelector?

I have come across numerous tutorials explaining the use of multiple :not selectors on a single element. However, I have yet to find any examples of using :not selectors on different levels within a single CSS / JS querySelector path. For instance, consider the following HTML structure:

<div class="anything_except_ignore">
    <input type="text" id="a" />
    <input type="hidden" id="b" />
</div>
<div class="ignore">
    <input type="text" id="c" />
</div>

I want to specifically select only input#a. I attempted the following in JS, but it did not yield the desired result:

var elems = document.querySelectorAll("div:not(.ignore) input:not([type='hidden'])");

In this scenario, it seems that the first :not selector is being disregarded (as both input#a AND input#c are being selected in the example above).

Is there a mistake in my syntax, or is it not possible to utilize :not selectors in this manner to solely select input#a?

I can find a workaround by examining the parent class in JS after the elements are selected, but I would prefer to address it correctly at the selector stage.

---UPDATE--- Upon suggestion in one of the responses, I misunderstood that other DIVs higher up in the DOM can meet the :not(.ignore) criteria, causing the input under the div.ignore element to be selected. By removing other DIVs, the intended logic does seem to function, as evident in this test:

var res="", elems = document.querySelectorAll("div:not(.ignore) input:not([type='hidden'])");
for(var i=0; i<elems.length; i++){
    res = res + elems[i].id + "\n";
}
alert(res);
<div class="anything_except_ignore">
  <div>
    <input type="text" id="aa" value="a"/>
    <input type="hidden" id="bb" value="b"/>
  </div>
</div>
<div class="anything_except_ignore2">
  <span>
    <input type="text" id="dd" value="d"/>
    <input type="hidden" id="ee" value="e"/>
  </span>
</div>
<div class="ignore">
    <input type="text" id="cc" value="c"/>
</div>

My apologies for any confusion, but this clarification should help understanding how it is supposed to work, as I could not find any other instances of this in my research.

---CONCLUSION--- As far as I can discern, I must either:

  • Ensure that my inputs are always direct children of the desired div and utilize the ">" syntax

  • After selecting all non-hidden inputs, use JS to examine the class name of all parent DIVs in the tree.

If there is a more effective querySelector method to address this specific issue, I am eager to learn about it.

Answer №1

There seems to be a common misconception about how the :not selector functions when combined with other selectors.

Could it be possible that there are more div elements present than what has been displayed to us?

div:not(.ignore) is not simply being "ignored", but rather the condition is being met by a different div element higher up in the hierarchy. The space between the two selectors signifies the descendant relationship, meaning that every input:not([type='hidden']) element that has a div ancestor without the class ignore anywhere above it in the DOM tree will match the entire selector.

If you modify your selector to

div:not(.ignore) > input:not([type='hidden'])
, it will specifically target input fields whose direct parent is a div without the class ignore.

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

Background color set for only half of the div

Is it possible to achieve a design similar to this using CSS, with a Half-background color for a div? ...

Tips for aligning a pseudoElement in the center below a bootstrap navigation link

Hello, I'm trying to align the line under my menu that was created using a pseudo element. Here is the HTML code: .nav-link.active { color: #482683; font-size: 14px; } .nav-link.active::after { content: ''; display: b ...

Height of the Accordion List

I have a code snippet below for an accordion list that I put together. I initially set the height for all the heading boxes to be uniform, but it seems like box A is displaying larger than the others. Can you help me figure out what went wrong? Any suggest ...

Inexplicably bizarre HTML glitch

I am currently utilizing a comment system that involves adding a new row to a SQL database. The system seems to be working fine; however, when I try to display the comments, the formatting of the comment div becomes all jumbled up. You can view the page wh ...

When you hit a certain point on the website, the scrolling momentarily pauses

Upon refreshing the page and scrolling down, I notice that the website experiences a brief lag for a few milliseconds before continuing as normal. Oddly enough, this issue only occurs after refreshing the page. Any suggestions on how to resolve this? Th ...

Troubles encountered with adapting apexcharts size within a react environment

As a novice front-end coder transitioning from a data analyst background, I am currently facing an issue with integrating an ApexChart visual into a flexbox element. The visual appears fine at a resolution of 2560x1440 pixels. However, upon further resizin ...

Aligning the page content in perfect harmony with the footer

I've encountered variations of this question in the past, but I haven't found a solution that fits my specific needs. My challenge involves having a footer fixed at the bottom of a page, even when the content doesn't fill the entire page. Ad ...

CSS - a collection of hyperlinks to browse. the pseudo-element a:visited::before is not behaving as anticipated

Here's what I'm looking to achieve: http://codepen.io/anon/pen/KaLarE I want to make a list of links, each with a checkbox in front. Then, I want to be able to check the boxes for visited links. I attempted this approach, but unfortunately, it ...

Python Selenium cannot locate button within an iframe

click here for imageI am trying to interact with the "바카라 멀티플레이" button located at the center of the website. Even after switching into an iframe, I am unable to detect the button. How can I achieve this? from selenium import webdriver im ...

Styling nested divs in CSS

I am experiencing an issue where the child divs within parent divs are overflowing outside of the parent divs. To get a better understanding of the problem, please try running the code below in a browser: My goal is to align the innermost divs horizontall ...

Adjusting table font dynamically using Angular and CSS

I am currently working on an Angular project and facing a challenge with setting the font size dynamically in a table. I have created a function to address this issue, which includes the following code snippet: $('.td').css({ 'font-size ...

Creating a seamless vertical marquee of several images that continuously scroll from bottom to top without interruption can be achieved by following these

Currently, I am seeking to design a mobile-friendly HTML page that features a border with multiple color images moving smoothly from bottom to top on both the right and left sides of the mobile screen. The transition should be seamless without any gaps o ...

Is there a way to specifically target the MUI paper component within the select style without relying on the SX props?

I have been experimenting with styling the Select MUI component using the styled function. I am looking to create a reusable style and move away from using sx. Despite trying various methods, I am struggling to identify the correct class in order to direct ...

What could be causing the HTML <DATALIST> dropdown to display next to its input rather than below it?

I have an input linked to a datalist, and it's functioning correctly. However, when I initially open the dropdown, the option list appears next to (right of) the input. After entering a few characters to narrow down the list, it then shifts below the ...

What are the steps for implementing a two-column layout in reveal.js?

When creating my slides using reveal.js, I write them in Markdown code. I am now looking to present my content (text, bulleted lists, or images) in a traditional two-column text layout. While I am open to a more complex initial setup, I want to ensure that ...

Manipulating a <DIV> style within an Angular 8 project

Looking to change the display style? Check out this template: <div style="display: none" #myDiv /> There are 2 methods to achieve this: Method 1: Directly if (1===1) this.myDiv.style.display = "block"; Method 2: Using @ViewChild @ViewChild(&apo ...

Customizing styles in ZK based on theme

I am currently working on a ZK application that has been styled using CSS, specifically the colors from the Sapphire theme. My goal is to have environment-dependent themes - Sapphire for production and Breeze for stage. While I have successfully implemente ...

Trouble with Setting a Background Image in Ionic with Javascript and Angular

I'm having trouble getting my background image to display in this Ionic project using CSS. It works when I embed it directly into the HTML, but that's not an ideal solution. I vaguely recall something about using base64 for images, but I'm n ...

Firefox is having trouble keeping <select> tags within their designated borders

I am currently developing a mobile app that requires the use of 3 <select> elements stacked on top of each other. It is crucial for these tags to align perfectly with each other and not extend beyond the boundaries of the background div. I have manag ...

"Switching Classes with a Click Event: A Step-by-

This script is designed to work with SoundCloud's widget in order to enable remote text buttons. I am attempting to modify it so that it functions as a remote for an image button that toggles between different pictures for pause and play, rather than ...