I’ve figured out how to get the width and hight of an image using javascript. My question is how do I apply the width and height to it’s nearest parent in this case it’s li using javascript.
<ul>
<li> <img src="img1.jpg" /> </li>
<li> <img src="img2.jpg" /> </li>
</ul>
The js i’m using:
$(window).load(function() {
var pic = $('img');
var w = pic.width();
var h = pic.height();
pic.removeAttr("width");
pic.removeAttr("height");
$('ul li').css('width',w);
});
Right now it is only getting the width and height of the first image and applying it to every li.
The solution below has been modified to demonstrate its effectiveness:
CSS:
HTML:
jQuery:
Example: http://jsfiddle.net/kfnhh/
Simply remove the click event handler and the code should be easily implemented in your environment.