View Full Version : <input type="image"> Can I get the width and height?

02-03-2009, 07:06 PM

I succesfully created a javascript function to auto resize the browser window according to the size of the image that is displayed. I retrieved the width and height via document.imagename.width and document.imagename.height. However, I wanted to add a server side onclick event (ASP.NET) - in order for this to work, I had to change the image into an imagebutton (onclick is not supported on an image web control in ASP.NET, that's specifically what the imagebutton control is for). In HTML this imagebutton control translates as an <input type="image"> This of course destroys my javascript, at least the part where I retrieve the correct width and height of the image.

So my question: is there a way to retrieve the width and height of an image used in an input type="image"? I tried document.formname.imagename.width but that didn't work.

02-04-2009, 01:35 AM
You can add runat="server" to the image and add it as a generic control on the server.

You can call your own __doPostBack("fooName",""); to make a callback.


02-04-2009, 04:34 PM
Thanks for the answer, Eric.

What did the trick, however, was document.getElementById("img").offsetWidth, and of course offsetHeight for height. This gave me the original dimensions of the image, which I needed.