MiscellaneousUser MiscellaneousUser - 7 months ago 46
HTML Question

How to reset image dimensions in javascript? (MVC)

I got the below javascript function that when you pass in the parameters, it'll load the picture and size it appropriately. Sometimes I won't know the height and size so therefore how do I reset the image's height (so the browser sizes the image appropriately) and width or delete the height and width properties so that images without passed in sizes aren't shrunk to 0,0 size? Want to avoid using JQuery.

function LoadPicture(picture,alt,text,height,width)
{
_img = document.getElementById('img')
_img.src = '/pic/' + picture + '.jpg';
_img.alt = alt;
_img.title = text;
if (height+'' != '' && height>0)
_img.height = height;
else {
_img.height = null;
}
if (width+'' != '' && width > 0)
_img.width = width;
else _img.width = null;

}

Answer

You could remove the inline styles, so default styling will "kick in":

if (height+'' != '' && height>0)
        _img.height = height;
    else {
        _img.removeAttribute("height");
    }