Forum Moderators: open

Message Too Old, No Replies

Set width of container element based on child image

Trying to create captions that don't exceed the width of the image above

         

illtron

6:46 pm on Aug 15, 2008 (gmt 0)

10+ Year Member



I've got a good idea about what needs to happen here, but I'm just not handy enough to figure it all out.

I've got divs that all have a class name and then images and paragraphs inside of the divs. The paragraph is a caption, and I can't think of any possible CSS-only way to make it so that it doesn't extend past the image.

Here's my HTML:


<body onload="setCaptions();">
<div class="container">
<h1>DOM test</h1>
<div class="photo">
<img src="joker.jpg" alt="Why so serious?" id="jokerpic" />
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</div>
</div>

</body>

I know I need to do something like "get the width of the images inside of divs, then set that value as the width of the div". This isn't complete, but it's as far as I keep getting before things get worse.

Here's the script from the <head>.


<script type="text/javascript">
function setCaptions() {
var picwidth = document.getElementById('jokerpic').clientWidth;
// alert(picwidth);
document.getElementsByClassName('photo').style.width = picwidth;
}
</script>

When it's not commented out alert(picwidth) does return the proper width of the image (300 pixels). But When I try to set the width of the div with the class "photo" to picwidth, I get an undefined value.

Any ideas?

birdbrain

9:56 am on Aug 16, 2008 (gmt 0)



Hi there illtron,

there is no document.getElementsByClassName(). :(

So you need to find the <div class="photo"> by other means. ;)

Try it like this...


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">

<script type="text/javascript">

window.onload=function() {
setCaptions();
}

function setCaptions() {

var dvs=document.getElementsByTagName('div');

var picwidth=document.getElementById('jokerpic').clientWidth;

for(var c=0;c<dvs.length;c++) {

if(dvs[c].className=='photo') {

dvs[c].style.width=picwidth+'px';

}
}
}

</script>

</head>
<body>

<div class="container">

<h1>DOM test</h1>

<div class="photo">

<img id="jokerpic" src="joker.jpg" alt="Why so serious?">

<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed
do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>

</div>
</div>

</body>
</html>


birdbrain

illtron

4:54 pm on Aug 18, 2008 (gmt 0)

10+ Year Member



This is good. I've got it working, and it's doing exactly what I was hoping. I'd like to take it a step further now.

Since this will be coming from a legacy system, I can't count on there being an ID in the image. Also, many pages will have multiple images like this. I think I'm going to have to get picwidth by tag name.

Here's what I've mangled the code into so far:


<script type="text/javascript">
window.onload=function() {
setCaptions();
}

function setCaptions() {
var containerDiv = document.getElementsById('container');
var dvs = containerDiv.getElementsByTagName('div');
var picwidth = dvs.getElementsByTagName('img')[0].clientWidth;
for (var c = 0; c < dvs.length; c++) {
if (dvs[c].className == 'photo') {
dvs[c].style.width = picwidth + 'px';
}
}
}

</script>

And the relevant HTML, which has changed a little. The container div has an ID now, and the images have none.


<body>
<div id="container">

<h1>DOM test</h1>

<div class="photo">
<img src="joker.jpg" alt="Why so serious?" />
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</div>

<div class="photo">
<img src="batman.png" alt="I'm Batman!" />
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</div>

</div>

</body>

illtron

5:49 pm on Aug 18, 2008 (gmt 0)

10+ Year Member



Just wanted to say that I noticed my typo in the getElementById('container'). No other progress though.

illtron

6:11 pm on Aug 18, 2008 (gmt 0)

10+ Year Member



A little more progress to report, but I'm afraid I'm not really going in the right direction.

This sort of works, but predictably only sets the width of the caption div to the width of the first image:


<script type="text/javascript">
window.onload = function() {
setCaptions();
}

function setCaptions() {
var containerDiv = document.getElementById('container');
var dvs = containerDiv.getElementsByTagName('div');
var picwidth = document.getElementsByTagName('img')[0].clientWidth;
for (var c = 0; c < dvs.length; c++) {
if (dvs[c].className == 'photo') {
dvs[c].style.width = picwidth + 'px';
}
}
}

</script>

birdbrain

6:21 pm on Aug 18, 2008 (gmt 0)



Hi there illtron,

try it like this...


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">

<script type="text/javascript">

window.onload=function() {
setCaptions();
}

function setCaptions() {
var picwidth=[];
var containerDiv=document.getElementById('container');
var dvs=containerDiv.getElementsByTagName('div');
for(var c=0;c<dvs.length;c++) {
if(dvs[c].className=='photo') {
picwidth[c]=dvs[c].getElementsByTagName('img')[0].clientWidth;
dvs[c].style.width=picwidth[c]+'px';
}
}
}

</script>

</head>
<body>

<div id="container">

<h1>DOM test</h1>

<div class="photo">
<img src="joker.jpg" alt="Why so serious?">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</div>

<div class="photo">
<img src="batman.png" alt="I'm Batman!">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</div>

</div>

</body>
</html>


birdbrain

illtron

8:18 pm on Aug 18, 2008 (gmt 0)

10+ Year Member



That's perfect! Thank you for your help on this!

birdbrain

8:40 pm on Aug 18, 2008 (gmt 0)



No problem, you're welcome. ;)