jQuery image captions

by Alex Kondys on March 4th, 2009 | Posted in Revium Sandbox | Comment on this entry

After seeing the work Adrian has done with jQuery over the past year or so, I figured it was time I started to get my head around it.

I wanted a way that the client could easily add image captions to the images they insert in their website without having to copy and paste a large amount of code just to get the effect required.

You can download the code here.

Of course, it could be neatened up a little, so feel free to grab the code and play with it as you like and post any revisions in the comments.

The jQuery function

This is the jQuery function that creates the required div’s and span’s to create the HTML output.

jQuery.fn.captions = function(value){
this.wrap("<div class='figure' style='width:"+this.attr("width");+"px;'></div>");
this.after("<span>"+this.attr("alt");+"</span>");
};

The breakdown

The following code creats a div around the image and sets the width if the image to the div so that when you float the div through CSS it floats correctly.

this.wrap("<div class='figure' style='width:"+this.attr("width");+"px;'></div>");

The following code then places a span after the image tag and inserts the value of the alt tag of the image.

this.after("<span>"+this.attr("alt");+"</span>");

The output

The following is an example of what is outputted.

<div class="figure">
<img class="figure" width="400" height="200" alt="Image caption" title="Image title" src="image.jpg"/>
<span>Image caption</span>
</div>

Using the plugin

To use the plugin, download the script and insert the following into the head area of your HTML document

<script type="text/javascript" src="jquery.captions.js"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$("img.captions").captions();
});
</script>

Then for the images you want to add captions too, just include the following class in your image.

class="captions"

Your image tag should look something like this

<img src="test.jpg" width="400" height="200" title="Batman logo" alt="Lorem ipsum dolor sit amet, consectetur adipisicing elit." class="captions" />

Dependencies

The plugin has been tested only with version 1.3 of jQuery.

Download the plugin

Download Captions v1.

Related posts:

  1. “Capitalise” jquery extension
  2. ASP.NET selectors in jquery
  3. ASP.NET Button – prevent double clicks part 2
  4. Degrading Script Tags
  5. Developing faster web forms in .NET

Tags: jquery

« ASP.NET MVC from Microsoft

Asp.Net MVC convert View to Word Document »

Leave a Reply

Click here to cancel reply.

Recent Articles

  • 10 Tips for user acceptance testing of web applications
  • Popular Content Management Systems
  • ASP.NET MVC [HandleError] and logging
  • VMWare hard lessons
  • Linq – SubmitChanges does not work
  • Using FourSquare for Marketing
  • ASP.NET Button – prevent double clicks part 2
  • The easy way to clear floated elements

Twitter

  • 50 Useful Tools & Resources For Web Designers <http://www.smashingmagazine.com/2010/07/26/50-useful-tools-and-resources-for-web-designers/> 3 days ago
  • Tourism Australia has launched a free "Go Ashes" app for iPhone to promote England vs Australia cricket series 4 days ago
  • #YouTube competition, on 24 July capture you Life In A Day http://youtu.be/tZFbDY3-eG4 1 week ago
  • More updates...

Revium Logo

  • Home
  • About
  • Expertise
  • Showcase
  • Contact

  • news
  • blog
  • sandbox
  • twitter
  • facebook
  • rss
Bookmark and Share

We are Revium, hear us roar!

The news.

23 Apr

Revium sponsors IIA CEO luncheon on 2010 Challenges

Last Wednesday 14th April Revium hosted a CEO luncheon for the Internet Industry Association (IIA). Revium have been long time supporters of the IIA and…

Continue reading
View archive

The blog.

11 Jun

VMWare hard lessons

Well, I learned a hard lesson today when I decided to give more hard drive space to one of our virtual machines. We run…

Continue reading
View archive

The sandbox.

02 Jul

10 Tips for user acceptance testing of web applications

1. Draw up a testplan Many testers would like to skip this step, but it is essential for structured testing. It depends of course on the…

Continue reading
View archive

eNewsletter.

 

© Copyright 2010. All Rights Reserved.

Revium Pty Ltd

e / info@revium.com.au t / Work +61 3 9429 2000

Find us: web development, seo

Disclaimer and privacy Revium Pty Ltd

10 Harvey Street
Richmond, Victoria, 3121 Australia
View map

Logo Partner MicrosoftLogo Partner KenticoLogo Partner IiaLogo Partner Iia RibLogo Partner Acs