PDA

View Full Version : Use a variable as to refer to a item in my doccument


Red-d
12-30-2004, 07:46 PM
Hi,
I have a very limited experience in javascript and I would like to know if it posible to use a variable to refer a object in my doccument as I would do with php for exemple.

There a sample of the code I want to use

<SCRIPT LANGUAGE=JavaScript>
function swapImage(id) {
id = 'IMG'+id;
switch (intImage) {
case 0:
"id".src = "http://example.com/images/flag_0.jpg"
intImage = 1
return(false);
case 1:
"id".src = "http://example.com/images/flag_1.jpg"
intImage = 2
return(false);
case 2:
"id".src = "http://example.com/images/flag_2.jpg"
intImage = 0
return(false);
}
}
</SCRIPT>


This function receive in parameter the number or the image I want to change the source and change it. I want to change the image with the name of the variable. For example: IMG227.

What im trying to do is to input a flag system to our help desk. Every line start with a image (which is a kind of flag) and when the user click on it, it change the flag color. So evry image has the name of the ID of the help thread of the line.

As it's my first time posting here I hope my question is clear ennough.

Thank you in advance

Bobo
12-31-2004, 03:05 AM
I don't really understand the question but I did notice an error in your script.

You have "id".src="~"
when it should be id.src="~"

Brandoe85
12-31-2004, 07:12 AM
I also don't understand really what your trying to do? And I don't see intImage defined either...

Red-d
12-31-2004, 03:38 PM
ok, I solved my problem with the help of annother programmer in there and there's the solution we came to, just in case ur interested:


<SCRIPT LANGUAGE="JavaScript">
function swapImage(elem) {
intImage = elem.id;
switch (intImage) {
case "0":
elem.src = "http://example.com/images/flag_0.jpg";
elem.id = 1;
return(false);
case "1":
elem.src = "http://example.com/images/flag_1.jpg";
elem.id = 2;
return(false);
case "2":
elem.src = "http://example.com/images/flag_2.jpg";
elem.id = 0;
return(false);
}
}
</SCRIPT>


and this is the php generating the images:


function formatFlag( $args )
{
return '<img border="1" id="'.$args['flag'].'" name="'.$args['id'].'"
src="http://example.com/images/flag_0.jpg"
onmouseup="swapImage(this);">';
}

$args['id'] is the name of the thread
and
$args['flag'] is the current collor of the flag

all this made me learn a lot about the use ellement in javascript. Thx you for ur time