Why do functional pseudos such as :not() and :has() allow quoted arguments?

This isn’t specific to :not(…) and :has(…) selectors- actually, all pseudos in Sizzle allow for quoted arguments. The pattern for pseudos’ arguments is defined as: pseudos = “:(” + characterEncoding + “)(?:\\((?:([‘\”])((?:\\\\.|[^\\\\])*?)\\2|([^()[\\]]*|(?:(?:” + attributes + “)|[^:]|\\\\.)*|.*))\\)|)” Which can be found on line 91 of sizzle.js as of 831c9c48… Let’s add some indentation to that, to … Read more

Can jQuery selectors be used with DOM mutation observers?

Yes, you can use jQuery selectors on data returned to mutation observer callbacks. See this jsFiddle. Suppose you had HTML like so and you set an observer, like so: var targetNodes = $(“.myclass”); var MutationObserver = window.MutationObserver || window.WebKitMutationObserver; var myObserver = new MutationObserver(mutationHandler); var obsConfig = { childList: true, characterData: true, attributes: true, subtree: … Read more

jQuery: Select data attributes that aren’t empty?

Just as further reference, and an up-to-date (may’14) (aug’15) (sep’16) (apr’17) (mar’18) (mar’19) (may’20) (jan’22)… Answer that works with: ###Empty strings: If the attr must exist & could have any value (or none at all) jQuery(“[href]”); ###Missing attributes: If attr could exist & if exist, must have some value jQuery(“[href!=”]”); ###Or both: If attr must … Read more

How do I select an element with special characters in the ID?

The problem is that brackets have special meaning in CSS (as attribute selectors) and dots do too, as class selectors. Try $.escapeSelector: $(‘#’ + $.escapeSelector(‘Punteggi[@counter].Descrizione’)) This will escape the selector so the special characters don’t affect the selection. You could also try using an attribute selector and wrapping that in quotes: $(‘[id=”Punteggi[@counter].Descrizione”]’) This will literally … Read more

jQuery selector to target any class name (of multiple present) starting with a prefix?

Because of the way the class attribute is designed, you’ll need to make use of at least two other attribute selectors (notice the whitespace in [class*=” detail-“]): $(‘a[class^=”detail-“], a[class*=” detail-“]’); This selects <a> elements with a class attribute that starts with detail-, or contains a class prefixed with detail-. Class names are separated by whitespace … Read more

What is the new proper way to use a child selector with a context node in jQuery?

The reason they are saying: Note: The $(“> elem”, context) selector will be deprecated in a future release. Its usage is thus discouraged in lieu of using alternative selectors. Is due to the comma followed by the context in the selector. E.g. $(“> elem”) is fine however, $(“> elem”, context) will be deprecated. $(“> elem”, … Read more

jQuery selector where text = some value

You can create your own selector methods For example, if you want to be able to do the following: $(‘.js-rating-label:hasText(unrated)’); You can define the hasText method as follows $.expr[‘:’][‘hasText’] = function(node, index, props){ return node.innerText.contains(props[3]); } props[3] contains the text inside the brackets after ‘:hasText’.