Archive Old post. Originally published 9 November 2012 on the WordPress version of this site. Preserved here for the search engines and the curious. Old me had opinions.
Developer 2 min read

Quick Tip: Remove IMG Height and Width with jQuery

As you may have noticed  my new site is fairly heavily responsive. A lot of extra work has gone into helping make the user experience for 50% of my visitors that use mobile devices. Now I love WordPress however it’s about time it started to move to accept modern methods like responsive design. That aside we can make a work around.

Using jQuery we can simply strip the attributes from images before being rendered. Now this will affect ALL images on your page.

jQuery(document).ready(function($) {
	$('img').removeAttr('width height');
});

Now obviously you need to get this into your theme somewhere. I personally have added it to my Global JS file which is already loaded. If you don’t have that you can simply create a file, call it whatever you like and enqueue the JavaScript in your theme’s functions.php.

function responsive_images() {
    wp_register_script( 'responsive-img', get_template_directory() . '/myfile.js', array( 'jquery' ), null, true );
    wp_enqueue_script( 'responsive-img' );
}

add_action( 'wp_enqueue_scripts', 'responsive_images' );

It’s not the best way of doing it, however it does work.


Filed under Developer. No comments, on purpose.