Sunday 8 August 2010

jQuery attribute equals selector using namespaces

First blog post in absolutely ages but thought I might just share this little bit of wisdom with the world. I've been rocking the jQuery endlessly recently on a big project and hit upon an annoying little snag over the last day or two.

jQuery selectors are brilliant, totally amazing, usually... now I've been getting well into using a local namespace for this site so I can throw endless data around in HTML attributes instead of being naughty and hiding data in rel attributes. This way I can put as much as I like in a tag i.e.

<span id="data" gl:cat_id="5">

or whatever then drag it out using some jQuery that looks like

$('#data').attr('gl:cat_id');

Great, easy, no problem. What I tried next was selecting by the custom attribute:

$('span[gl:cat_id="5"]')

Nope, no luck there... here's the pearl of wisdom: bung an escape in and happy days:

$('span[gl\\:cat_id="5"]')

and you're back on the road.

No comments:

Post a Comment