tags:

views:

30

answers:

3

I am developing a page to display an image, dependent on what the user has previously chosen from a menu. The name of the .jpg is passed in as a parameter ('photo') on the URL and I have been able to parse this as a global variable (myphoto). The .jpgs are all held in one folder.

I now need to do something like (I guess) quoting the image source as being

"myfolder/"+<script>document.write(myphoto)</script> 

but this is not working. Any ideas please?

(Image tag changed here to get round the anti-spam.)

BTW all client-side javascript. It's been a few years since I've used this!

+2  A: 

You can do the opposite and output the whole image tag in javascript:

<script language="javascript">
  document.write('<img src="myfolder/' + myphoto + '" />')'
</script>
Oded
Hi OdedYes, just tried that and it's worked a treat. Thank you!
Linnet
+2  A: 

It's not working because when the browser sees this line: <img src="myfolder/"+<script>document.write(myphoto)</script>, it's treating the + character as a character and not an operator.

You will need to programmatically set the src of the image. Something like this:

document.getElementById("myImage").src = "myfolder/" + myphoto;

or with jQuery:

$("#myImage").attr("src", "myfolder/" + myphoto);
SimpleCoder
A: 
document.getElementById("imgtagid").src = "your image folder/" + selectedPhoto
spinon