Skip to content

Instantly share code, notes, and snippets.

@mfields
Created May 6, 2011 01:40
Show Gist options
  • Save mfields/958312 to your computer and use it in GitHub Desktop.
Save mfields/958312 to your computer and use it in GitHub Desktop.
Add classes to images
( function( window, document, undefined ) {
var maybeAddClass = function( e, cn ) {
var c = e.getAttribute( 'class' );
if ( 0 == c.length ) {
e.className += cn;
}
else if ( -1 == c.indexOf( cn ) ) {
e.className += ' ' + cn;
}
return e;
}
var imgs, i, w;
imgs = document.getElementById( 'content' ).getElementsByTagName( 'img' );
for ( i = 0; i < imgs.length; i++ ) {
if ( 860 <= imgs[i].width ) {
imgs[i] = maybeAddClass( imgs[i], 'larger-than-860' );
}
else if ( 607 <= imgs[i].width ) {
imgs[i] = maybeAddClass( imgs[i], 'larger-than-607' );
}
else if ( 300 <= imgs[i].width ) {
imgs[i] = maybeAddClass( imgs[i], 'larger-than-300' );
}
/* Remove inline styles from image's caption wrapper element. */
if ( 'A' == imgs[i].parentNode.tagName ) {
var classAttr = String( imgs[i].parentNode.parentNode.getAttribute( 'class' ) );
if ( -1 != classAttr.indexOf( 'wp-caption' ) ) {
imgs[i].parentNode.parentNode.removeAttribute( 'style' );
}
}
/* Add a class to images that have been resized in the editor. */
if ( 'naturalWidth' in imgs[i] && imgs[i].naturalWidth != imgs[i].getAttribute( 'width' ) ) {
imgs[i] = maybeAddClass( imgs[i], 'forced-dimensions' );
}
if ( 'naturalHeight' in imgs[i] && imgs[i].naturalHeight != imgs[i].getAttribute( 'height' ) ) {
imgs[i] = maybeAddClass( imgs[i], 'forced-dimensions' );
}
}
} ) ( this, document );
@mfields
Copy link
Author

mfields commented May 6, 2011

Does anyone have thoughts on this javascript code? Just adding classes to certain images in the div with the id of "content". Feel free to yell at me about anything! Need help understanding best practices ... want to master javascript before 2012 ... or at least be completely comfortable with it! Thanks

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