I have found many examples explaining how to use multiple :not selectors on an individual element. However, so far I have been unable to find any examples that use :not selectors on different levels within a single selection path in CSS / JS querySelector.
For example, given a HTML structure of:
<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 wish to select JUST input#a.
I have tried the following in JS and it does not seem to work:
var elems = document.querySelectorAll("div:not(.ignore) input:not([type='hidden'])");
In this case it appears the first :not selector is ignored (so in the above example both input#a AND input#c are selected.
Have I made a mistake in the syntax or is this approach not possible to use :not selectors in this way to select just input#a?
I can work around this by looking a parent class in JS once the elements are selected, but I'd rather do it properly at the selector stage.
---UPDATE--- as suggested in one of the answers, my mistake was in not understanding that other DIVs further up the DOM can fulfil the :not(.ignore) criteria and so the input under the div.ignore element IS selected. If I remove other DIVs then the requested logic does seem to work as proved by 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>
Sorry for any confusion, but at least this helps clarify how it should work as I could find no other examples of this in my searching.
---CONCLUSION---
As far as I can tell I have to either:
make sure that my inputs are always immediate children of the div I want to check and use the ">" syntax
after selection of all non-hidden inputs, use JS to check up the DOM tree of each, to check the class name of all DIVs up the tree.
If there is a better querySelector way to solve this particular issue then I'd love to hear it.
This is a common misunderstanding of how :not works in combination with other selectors.
Let me guess, there are actually more div elements than you have shown us, around these elements …?
div:not(.ignore) is not “ignored”, but the condition is just fulfilled by a different element than you thought - by some div element further up the tree. The space in between the two partial selectors is the descendant selector, so every input:not([type='hidden']) element that has a div ancestor that doesn’t have the class ignore anywhere above it in the DOM tree, matches the full selector.
If you change your selector to div:not(.ignore) > input:not([type='hidden']), this would only select such input fields if their immediate parent is a div without that class.
Related
Here's my HTML setup:
<div class="form-item">
<input type="checkbox" id="my-check">
</div>
<div class="form-item">
<label for="my-check">I'm a checkbox</label>
</div>
Is there any way in all the world of CSS to style that label based on whether or not the checkbox is checked? (Without changing the current HTML structure?)
Unfortunately, you cannot use CSS to style your label based on the state of your checkbox without changing your HTML. As of now, CSS selectors support child selectors and sibling selectors, but no selectors to style the child of one element based on the child of another element. You can find the whole list of CSS element combinators at: http://www.w3.org/TR/css3-selectors/#combinators.
i have :
<form id="commentform">
<p class="class1">
<p class="class2">
<p>
<input id="captcha_code"></input>
</p>
</form>
i want to style the "p" which does not have nor id neither class, i want to do it in a way that it does not style the other "p" tags.
my "p" has a child with id (#captcha_code). i guess, if i can style #captcha_code parent, it will not concern the others... but i do not know how to do it...
There is not currently a way to style an element based on whether or not it contains a particular child. The best way I can think to style the <p> in your case would be:
#commentform p:not([class]){
}
Closing your paragraphs isn't completely necessary, but it's always nicer to read. Additionally, you shouldn't add a closing tag for your input in HTML:
<form id="commentform">
<p class="class1"></p>
<p class="class2"></p>
<p>
<input id="captcha_code">
</p>
</form>
JSFiddle
To keep it simple, this can't be done.
Unlike the child selector >, There is no (and will probably never be) a parent < selector in css.
Have a look at this article to see why.
Try this:
p > input {
// style for that..
}
Using this, the style will be applied to the parent of the input.
Fiddle for this: http://jsfiddle.net/afzaal_ahmad_zeeshan/WyV7A/
I'm using the following in some selenium code:
WaitForElement(By.CssSelector("#document-count:contains(<number greater than 0>)"));
Specifying the number greater than 0 is where I'm stuck. Is there any way to use only css to check and see if an element's innertext has something other than 0?
:contains was deprecated in CSS3. Since WebDriver ties directly into the browser, it's unable to use that pseudo-class.
Is there any way to use only css to check and see if an element's innertext has something other than 0?
Unfortunately not. CSS really screwed Selenium users over with their deprecation of both :contains and :nth
As Arran said, you can use xpath, or - if you are willing to experiment with C# and CSS together (not just css as you state) then you can come up with something to loop x amount of times checking the text.
Per Chris Coyier at CSS Tricks:
Deprecated
:contains() - As far as I know, this is gone. The current
CSS3 spec has removed it. I don't know the story, let me know if you
do. At a glance, it looks ridiculously useful (being able to select
objects based on the textual content they contain). It may be because
of problems, or having content in selectors being undesirable. My
preference would be to have it select by elements rather than text,
like p:contains(img), but alas, no such luck.
That said, if you were to set the value properties, you may be able to use :not([value="0"]):
See jsFiddle demo
HTML
<div id="doc">
<input type="text" value="0" />
<br />
<input type="text" value="1" />
<br />
<input type="text" value="2" />
</div>
CSS
#doc input[value="0"]
{
background: red;
}
#doc input:not([value="0"])
{
background: green;
}
Result
Regarding the code here:
The radio buttons in the fiddle aren't acting the way radio buttons should.
I'm not familiar with + in CSS and I wonder if this has something to do with it.
input[type="radio"]:checked+label{ ..... }
//a label that immediately follows an input of type radio that is checked
The + sign in CSS denotes a selector that is adjacent to the first selector.
In your example the + sign is modifying the label not the actual radio button but the label only.
Now it is not working because you havent specified a group name (or name) for the radio button try the following html.
<form>
<input type="radio" id="radio1" name="rdo">
<label for="radio1"></label>
<input type="radio" id="radio2" name="rdo">
<label for="radio2"></label>
</form>
Now the radiobuttons are bound to the same name and will function correctly.
Cheers, Nico
This selects the adjacent element, see The 30 CSS Selectors you Must Memorize.
FTA:
This is referred to as an adjacent selector. It will select only the element that is immediately preceded by the former element. In this case, only the first paragraph after each ul will have red text.
Supported in IE7+
It matches a element immediately preceded another specified element.
In your example this would mean it matches any label element that follows an input element of type="radio".
More information on selectors here: http://www.w3.org/TR/CSS2/selector.html
If you are referring to the radio buttons not being mutually exclusive (you can select multiple ones), it is because the html does not define the radio buttons as being part of the same group. Use the name attribute to make this happen.
<form>
<input type="radio" name="radioGroup" id="radio1">
<label for="radio1"></label>
<input type="radio" name="radioGroup" id="radio2">
<label for="radio2"></label>
</form>
I believe your questions about the + in CSS is unrelated, but it is correctly identified as the next sibling in other answers.
Is there any difference in:
#example .test{margin-top:10px}
And
#example span.test{margin-top:10px}
.test is totally unique, no any class with that name exists. I have been trying to use like
<div id="example">
<label for="name">Name</label>
<input name="name" type="text" />
<span class="test">asdfghjkl</span>
</div>
The first css won't work, while the second works fine. Just wondering why first wont work?
Thanks
Edit:
I found out that in the input field (in above example), i had put float:left;, which was causing the problem. If anyone is interested, here you can see example. If you remove float:left;, it will work fine.
http://jsfiddle.net/CmXrX/1/
Yes, there is a difference. That difference is in specificity. If you have conflicting CSS rules -- two rules point to the same element but certain properties conflict -- the conflict will be sorted out by the rules about specificity.
Each type of selector has a certain number of "specificity points" For instance:
Selector type Points
-----------------------------
HTML selector 1
class selector 10
id selector 100
So if you had these two rules:
#example .test{margin-top:10px}
#example span.test{margin-top:20px}
the top margin would be 20px, because the second rule has a specificity of 111 (100 + 10 + 1) whereas the first has 110 (100 + 10).
My guess, therefore, is that you have a conflicting style somewhere that has 111 specificity.
There are two differences:
The latter will only apply, if the class test is applied to a span and no other element.
(and most likly more relevant in your case) the latter has a higher specificity than the former.
You probably have an other rule with a higher specificity then the first selector but lower than the second. That means this other rule will override the first rule, but not the second.
I found out that in the input field (in above example), i had put float:left;, which was causing the problem. If anyone is interested, here you can see example. If you remove float:left;, it will work fine. http://jsfiddle.net/CmXrX/1/
yes there is a difference :
<div id="example">
<label for="name">Name</label>
<input name="name" type="text" />
<span><div class="test">asdfghjkl</div></span>
</div>