...

View Full Version : using a variable in a path



Cooter
01-14-2004, 10:56 PM
Hi -

I have three graphics, and each has an associated link that - when clicked - will increase the width of the graphic. I'm trying to write a function that passes a variable (which of the three has been clicked) which would widen the appropriate bar. In a nutshell I have:

<head>
<script language="javascript">
function change(a) {
var mybar = 'document.bar'+a+'.width';
mybar = mybar + 1;
}
</script>
</head>
<body>
<img src="box.jpg" name="bar1" width="10"><a href="#" onclick="change('1');">change</a> <br>
<img src="box.jpg" name="bar2" width="10"><a href="#" onclick="change('2');">change</a> <br>
<img src="box.jpg" name="bar3" width="10"><a href="#" onclick="change('3');">change</a> <br>
</body>

The only way I was able to get this to work was to create an if..else if statement that checked for each number. Only thing is this could grow to many more than three items, so I wanted a function that checks everything then increases the width in the appropriate bar. New to javascript, and any help is much appreciated....thanks

- cooter

COBOLdinosaur
01-15-2004, 01:17 AM
Try:

<script language="javascript">
function change(a) {
str='bar'+a;
var mybar = 'document.images[str].width';
mybar = mybar + 1;

glenngv
01-15-2004, 06:16 AM
function change(a) {
document.images['bar'+a].width++;
}

Cooter
01-15-2004, 04:45 PM
thanks to both. I'm back on track...

one question, did you use 'images' in the path because of the variable right after it? Is that something that should be done even if you know the image's name?? just wondering...

- cooter

COBOLdinosaur
01-15-2004, 11:27 PM
Images is the collection of images in the document object. If you just reference the name it will not work in all browsers, and in some browsers you may not be able to refrence all the attributes

document.somename come from the Microsoft sloppy coding school. They encourage its use because the page break in most non-Microsoft browsers.

Anytime you abberiated the DOM references you will have problems with browsers that follow the standards.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum