Tuesday, August 9th, 2011
A simple random bit on var selector
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.











Hey guys, just to make my marketing people happy… I actually work for Isobar, I just write for IBM on occasion.
Thanks!
Apologies, Rob. Corrected.
Seriously, no post since 1 month, and you post à article about someone suggesting to use vars ? Isn’t that what variables are made for ?
And what’s the big deal?
Yeah, not so cool. And the “article” doesn’t even give a good example – or one I can understand in the 2 seconds I’d give.
At least a “instead of X I do Y” would be useful.
Hi guys,
what’s wrong with Ajaxian ?, this is not what it was before. Now there are only stupid pointless articles. It’s become really boring blog. It’s a pity
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!
Hey Ajaxian! Why would you post this rubbish? Are your editors high? It’s utterly pointless, beyond quaint, and way too longwinded. 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.
What the bloody hell was that?
R.I.P. Ajaxan.
Although not as in depth with its news, I’ve started a new web developer news site called http://devbullet.in which offers brief news updates of what’s new on the web.
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.
I’ve been doing this for a VERY long time. you don’t need a special attribute.
Use the data- attributes. I actually use one called data-selector.
THEN you build your selector like this $(‘[data-selector=foo]’)
wow.
Will have agree with @jaimz