Adding a Toggle to jQuery LiteLighter

Of course, as you know jQuery Litelighter doesn’t come with a toggle by default. By toggle I am referring to a link to turn on and off the syntax highlighting so you get a clean text for copy and paste. Litelighter was built to be super light weight and easy to theme, so you have a great opportunity to design something for yourself.

First, lets attach a Litelighter to some code:

// the overwhelmingly difficult init
$('pre').litelighter();

Not too difficult. Now we want to place a link above each pre to toggle. With jQuery this is dirt simple:

After we have attached Litelighter we just go through each one and add a link before. How easy is that!? We could theoretically avoid the each() and just use before(), but then we would have to traverse to find the pre. If you want to see what it looks like, well, you’re looking at it. The new blog design implements this.

If you would like to learn more about jQuery Litelighter for syntax highlighting visit the documentation. Enjoy!

You found me, I'm Trent. I develop practical things for the web, like javascript libraries and plugins. I also love databases so don't talk trash about SQL. Beyond that I post about useful web development stuff here on my blog.

Feel free to subscribe to learn about project updates, tutorials, and more!