1

I'm replacing one image with another in javascript, then adding a link to it, but it doesn't seem to be working. Any suggestions?? Please and thank you!!

function showImage2(){
    document.getElementById("tbc").src = "images/s2.jpg";
var elem = document.getElementById("Slideshow");
    elem.style.display = "none";
    document.getElementById('tbc').style.display='block';
document.getElementById('tbc').style.usemap='ss2Map'; 
var link = document.createElement('a'); // create the link
    link.setAttribute('href', 'wastewater.html'); // set link path
    link.appendChild("images/s2.jpg"); // append to link
}
3
  • How is it not working? Do you get an error? Commented Jul 23, 2013 at 15:15
  • Can you post your HTML markup as well? Sometimes the answer is a simple misspelling of an ID Commented Jul 23, 2013 at 15:15
  • You code is creating HTML in-memory, but never actually inserts these into the DOM. Commented Jul 23, 2013 at 15:16

3 Answers 3

2
link.appendChild("images/s2.jpg"); // append to link

This line won't do anything. You can only append an element, not a text string. You need to append document.getElementById("tbc") instead if I understand your markup correctly.

If that's not what you're trying to append, you can use var el = document.createElement('img') to create an img tag and then set the src attribute using el.setAttribute('src','images/s2.jpg')

After this, the above line would become link.appendChild(el); which would work.

Sign up to request clarification or add additional context in comments.

Comments

1

I think all you really need is to have one image with a link and one image without the link. Onload, the image without the link is shown and the other image with the link is hidden. Once click on a button or something, then hide the image without the link and show the image with the link correct?

<img id="image1" src="http://us.123rf.com/400wm/400/400/tonygers/tonygers1108/tonygers110800022/10200687-manipulated-nasa-photographs-of-the-earth-and-moon-isolated-together-on-a-black-background.jpg" />
<a style="display:none;" id="image2" href="http://www.google.com" target="_blank"><img  src="http://d1jqu7g1y74ds1.cloudfront.net/wp-content/uploads/2011/08/us-astronaut-bruce-mccandless-space-walk.jpg" /></a>
<a href="javascript:void(0)" onclick="showImage2()">Click Me</a>
<script>
    function showImage2(){
        var imageOne = document.getElementById('image1');
        var imageTwo = document.getElementById('image2');
        imageTwo.style.display = 'block';
        imageOne.style.display = 'none';
    }   
</script>

You can see the working code here. http://jsfiddle.net/QbbJU/1/

Comments

0

appendChild() can only take a DOM node, not a string.

To set the text of an element, you can either set innerHTML or textContent, or append a text node (from document.createTextNode())

You also probably want to put the link somewhere in your page.

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.