jQuery For Designers: Part 1

by Mark on April 18, 2007

I’m a bit funny when it comes to semantic markup. I really don’t like putting visual cues into the markup that I may want to change later. You know those little symbols that we put at the end or beginning of a sentence, that we sometimes no longer use as literary symbols.

As an example, when I’m creating a form it pains me that I have to embed a colon into the markup at the end of the label text. We know why we do this, so that our input box has some kind of “stop” before it, making the view to the user just that little bit cleaner and easier on the eye. Then of course the client asks us if we can change the colon to a hyphen or a comma and we huff and puff because we’ve embedded these visual cues into the mark-up of all of our pages and doing a search and replace on a colon in an entire site can be a bit hit and miss at the best of times.

CSS to the rescue?

Those thoughtful folks from the W3C have given us the lovely pseudo classes of “:before” and “:after” that we can use to append or prepend content before or after an element. But as you may know these really useful tools are not featured in Internet Explorer. So what next?

Let’s try JavaScript?

Nope, I’m a designer, I don’t get it, its ugly, its not accessible, bad for usability and it’ll bloat my page. Hmm OK.

Mythical widget

Let’s pretend for a minute that there’s this mythical widget thingy, that we can put into our mark-up and it’ll do all sorts of clever things in a very easy manner without us having to be a programming whizz and it won’t damage our nice clean mark up. Let’s also assume that our mythical widget thingy looks like this.

$(“dd label”).prepend(“:”);

It couldn’t really be any simpler and we haven’t even touched our beautiful mark-up.

Of course we want this to be site wide, so we just put this JavaScript into a separate file and include this in every page.

<script src=”http://jquery.com/src/jquery-latest.js”></script>
<script src=”scripts/myjavascript.js”></script>

Now every page that has a “dd” element with a label, will get a colon stuck on the end. When our client asks to change this we can just do a very minor edit and the whole site is updated, just like it should.

The downside to this technique is that the user needs to have JavaScript enabled, but that’s a risk that I’m prepared to take as the majority of users do and in this case it degrades quite nicely too.

As you can see the power of jQuery is pretty phenomenal and we haven’t even scratched the surface. For more information on jQuery take a look at the jQuery homepage.

In Part 2 we’ll look at a few more examples based on this technique. How about using an image instead of a colon? Or replacing the actual label text with an image? or…?

{ 11 comments… read them below or add one }

John Resig May 9, 2007 at 2:38 pm

You can read a full version of this tutorial here:
http://docs.jquery.com/Tutorials:jQuery_For_Designers

Mark May 10, 2007 at 2:38 am

Hi John, thanks for popping by. :) The original article on markpanay.com is going to be removed soon and migrated here, so we’ll need to update the JQuery site with this URL, hope that’s ok?

John Resig May 10, 2007 at 3:25 pm

@Mark: Sure – it looks like someone already updated the wiki page too – that was fast!

Ivan October 23, 2008 at 6:15 pm

great tutorial!

mikon December 2, 2008 at 7:32 pm

I am real manager for real company Invest UK!,

Adam March 31, 2009 at 1:05 pm

Great well written tutorial with swathes of common sense

yashmistrey October 27, 2009 at 11:31 pm

This is very impressive, i have a question

How a home page fade in before launch / load ?
Please ! is there any solution for that ?

edygalantzan January 26, 2010 at 1:40 am

Were can I find nice examples?

Serkan Yildiz January 27, 2010 at 11:56 am

Test :D

Serkan Yildiz January 27, 2010 at 7:56 pm

Test :D

vickychandnani March 9, 2010 at 8:38 am

how 2 download toutorial fast

Leave a Comment

{ 2 trackbacks }

Next post: