Skip to content

Instantly share code, notes, and snippets.

@ajtroxell
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ajtroxell/c73a40992f4191bf5db2 to your computer and use it in GitHub Desktop.
Save ajtroxell/c73a40992f4191bf5db2 to your computer and use it in GitHub Desktop.
Image Captions in DNN with jQuery
$('.ModDNNHTMLC img').each(function(i) {
var img = $(this).css('margin', ''),
width = $(this).css('width'),
imageCaption = $(this).attr('longdesc'),
figure;
if (typeof imageCaption !== typeof undefined && imageCaption !== false) {
if (img.css('float') == 'left' || img.hasClass('alignleft')) {
img.css('float', '');
figure = $('<figure class="alignleft"></figure>').css('width', width).css('min-width', '100px');
img.removeClass();
img.wrap(figure);
img.parent('figure').append('<figcaption>' + imageCaption + '</figcaption>');
img.attr('src', $(this).attr('src')+'?'+Math.random());
} else if (img.css('float') == 'right') {
img.css('float', '');
figure = $('<figure class="alignright"></figure>').css('width', width).css('min-width', '100px');
img.removeClass();
img.wrap(figure);
img.parent('figure').append('<figcaption>' + imageCaption + '</figcaption>');
img.attr('src', $(this).attr('src')+'?'+Math.random());
} else if (img.css('float') == 'none' || img.hasClass('aligncenter')) {
img.css('float', '');
figure = $('<figure class="aligncenter"></figure>').css('width', width).css('min-width', '100px');
img.removeClass();
img.wrap(figure);
img.parent('figure').append('<figcaption>' + imageCaption + '</figcaption>');
img.attr('src', $(this).attr('src')+'?'+Math.random());
}
}
});
@ajtroxell
Copy link
Author

Updated

  • Prevent images with empty longdesc fields being parsed.
  • Add minimum width to images to force display.
  • Refresh image src immediately after display because of above problem where image will display but not populate any space. Forcing refresh of src by adding random string fixes this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment