The CSS Selector for an Element That Its Id Is in The "Foo:Bar" Form

The CSS selector for an element that its id is in the foo:bar form

According to the W3c spec linked to in this answer, using colons in element IDs is plain wrong if used unescaped.

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-z0-9] and ISO 10646 characters U+00A1 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance, the identifier "B&W?" may be written as "B\&W\?" or "B\26 W\3F".

Is the JSF framework really outputting those IDs in the final rendered HTML?

CSS selector for foo that contains bar?

To select all OBJECT containing PARAM, in CSS:

OBJECT:has(PARAM)

To select all OBJECT having a direct child PARAM, in CSS:

OBJECT:has(> PARAM)

No, what you are looking for would be called a parent selector. CSS has none; they have been proposed multiple times but I know of no existing or forthcoming standard including them. You are correct that you would need to use something like jQuery or use additional class annotations to achieve the effect you want.

Here are some similar questions with similar results:

  • Is there a CSS parent selector?
  • CSS Parent/Ancestor Selector
  • Complex CSS selector for parent of active child

How to use JSF generated HTML element ID with colon : in CSS selectors?

The : is a special character in CSS identifiers, it represents the start of a pseudo class selector like :hover, :first-child, etc. You would need to escape it.

#phoneForm\:phoneTable {
background: pink;
}

This only doesn't work in IE6/7. If you'd like to support those users as well, use \3A instead (with a trailing space behind!)

#phoneForm\3A phoneTable {
background: pink;
}

Above works in all browsers.


There are several other ways to solve this:

  1. Just wrap it in a plain HTML element and style via it instead.

     <h:form id="phoneForm">
    <div id="phoneField">
    <h:dataTable id="phoneTable">

    with

     #phoneField table {
    background: pink;
    }


  2. Use class instead of id. E.g.

     <h:dataTable id="phoneTable" styleClass="pink">

    with

     .pink {
    background: pink;
    }

    or

     table.pink {
    background: pink;
    }

    Additional advantage is that this allows much more abstraction freedom. The CSS is reusable on multiple elements without the need to add selectors and/or copypaste properties when you want to reuse the same properties on another element(s).



  3. Since JSF 2.x only: change the JSF default UINamingContainer separator by the following context param in web.xml. E.g.

     <context-param>
    <param-name>javax.faces.SEPARATOR_CHAR</param-name>
    <param-value>-</param-value>
    </context-param>

    So that the separator character becomes - instead of :.

     #phoneForm-phoneTable {
    background: pink;
    }

    Disadvantage is that you need to ensure that you don't use this character yourself anywhere in the ids and this is thus a very brittle approach. I do not recommend this approach. This is a bad practice.



  4. Since JSF 1.2 only: disable prepending of the form id.

     <h:form prependId="false">
    <h:dataTable id="phoneTable">

    so that you can use

     #phoneTable {
    background: pink;
    }

    Disadvantage is that <f:ajax> won't be able to find it and that it is considered poor practice: UIForm with prependId="false" breaks <f:ajax render>. I do not recommend this approach. This is a bad practice. Moreover, this attribute does not exist in all other UINamingContainer components, so you still have to deal with them the right way (#1 and/or #2 here above).



In your specific case, I think turning it into a CSS class as described in #2 is the most appropriate solution. A "phone table" namely doesn't seem to represent a website-wide unique element. Real website-wide unique elements such as header, menu, content, footer, etc are usually not wrapped in JSF forms or other JSF naming containers, so their IDs wouldn't be prefixed anyway.

See also:

  • How to select JSF components using jQuery?
  • By default, JSF generates unusable IDs, which are incompatible with the CSS part of web standards

I can't find any documentation on this CSS selector.. what is it?

#foo.bar refers to the element with together foo id and bar class.

$('#foo.bar').fadeOut('slow');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div id='foo' class='bar'>box</div>

What's the difference between CSS classes .foo.bar (without space) and .foo .bar (with space)

I think you got a slight misunderstanding what the first one means.

.element .symbol {}

Means that those CSS settings are applied to any HTML element with the class .symbol that is inside an element with the class .element.

<div class="element">
<div class="symbol" />
</div>

In this example your first CSS entry would affect the <div> tag in the middle.

Your second example means that the first class requires two classes to be affected. Other than that it's equal to the first one.

<div class="element large">
<div class="symbol" />
</div>

So if the HTML looks like this, the CSS values will be applied to the inner <div> tag as well.

If you want to set CSS tags that apply for multiple classes separately then you need to split them up using a comma. So it looks like this:

.element, .symbol {}

Edit: By request the link to the documentation of the CSS selectors.

CSS conditional selector either this or that

you could give them a class and then check the id of the target element:

$('').on('click', function(e) {  if (e.currentTarget.id === 'foo') {    console.log('foo clicked');  } else {    console.log('bar clicked');  }});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div class="click" id="foo">foo</div><div class="click" id="bar">bar</div>

jQuery or CSS selector to select all IDs that start with some string

Normally you would select IDs using the ID selector #, but for more complex matches you can use the attribute-starts-with selector (as a jQuery selector, or as a CSS3 selector):

div[id^="player_"]

If you are able to modify that HTML, however, you should add a class to your player divs then target that class. You'll lose the additional specificity offered by ID selectors anyway, as attribute selectors share the same specificity as class selectors. Plus, just using a class makes things much simpler.

Why selecting by ID is not recommended in CSS?

CSSLint gives a guide to why they make their recommendations:

IDs shouldn't be used in selectors because these rules are too tightly coupled with the HTML and have no possibility of reuse. It's much preferred to use classes in selectors and then apply a class to an element in the page. Additionally, IDs impact your specificity and can lead to specificity wars.

(From Disallow IDs in selectors.)

Basically, if you structure your code to use classes rather than IDs, your code can be more general and reusable, which is generally a good thing. Furthermore, specificity is a hard thing to get your head around, and can cause bugs that are hard to find, so if you omit ID selectors, you're less likely to have conflicting rules resolved in unexpected ways.

How to select html nodes by ID with jquery when the id contains a dot?

@Tomalak in comments:

since ID selectors must be preceded by a hash #, there should be no ambiguity here

“#id.class” is a valid selector that requires both an id and a separate class to match; it's valid and not always totally redundant.

The correct way to select a literal ‘.’ in CSS is to escape it: “#id\.moreid”. This used to cause trouble in some older browsers (in particular IE5.x), but all modern desktop browsers support it.

The same method does seem to work in jQuery 1.3.2, though I haven't tested it thoroughly; quickExpr doesn't pick it up, but the more involved selector parser seems to get it right:

$('#SearchBag\\.CompanyName');

Mojolicious Tests: How to get the input value using css-selectors

From the Test::Mojo

The text_is - Checks text content of the CSS selectors first
matching HTML/XML element for exact match with at in Mojo::DOM.

The at method

Find first element in DOM structure matching the CSS selector and
return it as a Mojo::DOM object or return undef if none could be
found. All selectors from "SELECTORS" in Mojo::DOM::CSS are supported.

And the

html body form div#adatum input#angebotsdatum value
^^^^^^ - this isn't valid

IMHO, isn't valid CSS selector.

You can try the next (shortened) selector:

div#adatum > input[value="2014-02-22"]

so find the element <input> what has an attribute equal to 2014-02-22.

E[foo="bar"]
An E element whose foo attribute value is exactly equal to bar.

More info Mojo::DOM::CSS#SELECTORS

So, it is enough test the existence of the element exactly with the wanted value:

->element_exists('input[value="2014-02-22"]', '...');

Ps: I'm not an very experienced Mojo developer, so ...



Related Topics



Leave a reply



Submit