0

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
  • What code have you got so far? Commented Jul 4, 2010 at 20:46
  • Just format your code as code (by intending it with four spaces or by using backticks ` ) and it is displayed correctly ;) Commented Jul 4, 2010 at 20:46

3 Answers 3

2

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

<script language="javascript">
  document.write('<img src="myfolder/' + myphoto + '" />')'
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Oded Yes, just tried that and it's worked a treat. Thank you!
2

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);

Comments

0
document.getElementById("imgtagid").src = "your image folder/" + selectedPhoto

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.