Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-15-2009, 08:18 PM   PM User | #1
theflyingminstr
Regular Coder

 
Join Date: Jul 2007
Posts: 191
Thanks: 0
Thanked 0 Times in 0 Posts
theflyingminstr is an unknown quantity at this point
Change Width & Height Dynamically of Div

Hi. This script changes the image, I wanted to know if there is a way to automatically resize the div's height and width based on the actual image proportions.

Thank you

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Switch Picture</title>

<script type="text/javascript">
function changeIt(imageName,objName)
{
var obj = document.getElementById(objName);
var imgTag = "<img src='"+imageName+"' width='100%' height='100%' border='0' />";
obj.innerHTML = imgTag;
return;
}
</script>

</head>

<body>

<div id="change" style="width:200px; height:200px;">
<img src="cat.jpg" width="100%" height="100%" border="0" />
</div>

<br/>

<a href="#" onclick="changeIt('dog.jpg','change');">Switch to Dog Pic</a>

</body>

</html>
theflyingminstr is offline   Reply With Quote
Old 07-16-2009, 06:51 AM   PM User | #2
fside
Regular Coder

 
Join Date: Mar 2008
Posts: 301
Thanks: 2
Thanked 30 Times in 30 Posts
fside is an unknown quantity at this point
You are using percentages because you don't know the height and width of the next image? Whether you use some proportional aspect, or fit the border right to the image, I believe all modern browsers include "width" and "height" properties for "img" elements. Just give it an "id" attribute so you can find it. And you should be able to read height/width and adjust your border as you wish.
fside is offline   Reply With Quote
Old 07-16-2009, 03:20 PM   PM User | #3
theflyingminstr
Regular Coder

 
Join Date: Jul 2007
Posts: 191
Thanks: 0
Thanked 0 Times in 0 Posts
theflyingminstr is an unknown quantity at this point
Thank you, that's very helpful advice
theflyingminstr is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:05 AM.


Advertisement
Log in to turn off these ads.