Isobar’s Rob Larsen suggests that there is often a need to build CSS selectors dynamically when building applications. ”This is typically some existing pattern paired with a loop counter or something pulled from a data attribute,” he writes on his blog. His choice is to create a variable called ”selector” and ”to craft the selector on its own line.” This is then passed into jQuery. He shows a simple example.

The author’s e.g. is not valid JavaScript. Whichever noob has never heard of variables or string concatenation will surely not gain any insight from typing that “example code” into their interpreter, because it will only throw a SyntaxError. FAIL!

I actually registered to leave a comment. I can’t agree more with the other commenters. What happened to this website? Hardly any new articles, and all the new articles are of very poor quality. It’s a shame really.

Every use of “selector concatenation” I’ve ever seen was just poorly formed javascript and a lack of understanding of how selectors work. In short, it’s a huge anti-pattern.

Here’s a tip: If you’re generating divs with unique ids en-mass, give them a class, or give their *parents* an ID/class. You don’t need to have jQuery post-fix 400 separate elements. That’s stupid, slow, and for CSS yields a FOUC.

We use cookies and other similar technologies (Cookies) to enhance your experience and to provide you with relevant content and ads. By using our website, you are agreeing to the use of Cookies. You can change your settings at any time. Cookie Policy.