Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Want to display an image when clicking a link.

CodeNewbie

New Coder
Hi all,

I'm very new to coding and very green with JS, so please bear with me :)

I simply want to have a row of links at the top of a page (maybe 4 or 5 links) that when clicked on display an image below them that changes with each link press. I would also like to display some text at the bottom of the image. E.g. Link1 is "Grey Cat", when clicked shows a pic of a grey cat with text below that says "Grey Cat". Link2 is "Black Cat" etc.

This is as far as I got:

JavaScript:
    <a href="#" onclick="this.src='https://upload.wikimedia.org/wikipedia/commons/3/3a/Russian_blue.jpg'" this.innerHTML = 'Grey Cat Wiki';>Grey Cat</a>

Any advice would be greatly appreciated!
 

Attachments

  • JS.jpg
    JS.jpg
    390.7 KB · Views: 5
I would not try doing this in the anchor, I would call a function instead.
1) Load all images in a div and have their visibility turned off and their z-index all set to 1.
2) Have all anchors call the same function and pass the id of the calling link sent to the func.
3) Have the function set all images to display:none; and their z-index to 1. Then for the calling image bring the z-index to the front and display to block
 
Back
Top Bottom