Tuesday, August 9th, 2011

A simple random bit on var selector

Category: CSS, jQuery

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.

Posted by jvaughan at 7:26 pm

3.1 rating from 310 votes


Comments feed TrackBack URI

Hey guys, just to make my marketing people happy… I actually work for Isobar, I just write for IBM on occasion.


Comment by roblarsen — August 9, 2011

Apologies, Rob. Corrected.

Comment by jvaughan — August 9, 2011

Seriously, no post since 1 month, and you post à article about someone suggesting to use vars ? Isn’t that what variables are made for ?

Comment by Korri — August 9, 2011

And what’s the big deal?

Comment by zakovyrya — August 9, 2011

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.

Comment by tarwin — August 10, 2011

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

Comment by auspex — August 10, 2011

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!

Comment by humanchimp — August 10, 2011

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.

Comment by gregorybolkenstijn — August 16, 2011

What the bloody hell was that?

R.I.P. Ajaxan.

Comment by fritzthecat — August 17, 2011

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.

Comment by AndiSmith — August 25, 2011

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.

Comment by stefankendall — September 9, 2011

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]’)


Comment by jaimz — February 6, 2012

Will have agree with @jaimz

Comment by chandan20j — March 14, 2013

Leave a comment

You must be logged in to post a comment.