Test img:first-child' ).forEach( function ( img ) { if ( ! img.title ) { return; } var wrap = document.createElement( 'div' ); wrap.className = 'post-image-wrap'; img.parentNode.insertBefore( wrap, img ); wrap.appendChild( img ); var caption = document.createElement( 'div' ); caption.className = 'post-image-caption'; caption.textContent = img.title; wrap.appendChild( caption ); } ); } );