1

Yes the

 <Input type='Button'>,

suggested by Mike made the difference.

Edited code below

This is a follow on question to

HTML5, div, hidden, show on click

I'm trying to get a working example, based on the last answer in that post.

Still confused about how I should have posted this...Should I have posted my question as a 'comment' in response to original post I followed? Or do I create a posting when it's a new (I think) question? There's also the other posting that Mike recommended, that cleared up the problem.

I'd like to be able to 1) add the lightbulb image- 'WebVuCoolOldBulb-2.jpg' to the DOM (did I use 'DOM' right?)

and ALSO understand how to 2) Replace my cactus picture - img/WebVuHedgehogCalicoCactusInBloomJoshuaTreeLaurelShimer.jpg with the picture of the lightbulb - WebVuCoolOldBulb-2.jp

The code runs clean with no errors from 'Show Error Console' in Safari. But it does not add the light bulb image

screen shot cactus photo plus button

<html>
<body>
<form>

  <img id="cactusPhoto" 
       src="img/WebVuHedgehogCalicoCactusInBloomJoshuaTreeLaurelShimer.jpg" 
       alt="Picture from Joshua Tree."
       height="100" width="100" >

  <button 
       type=button
      onclick="show_image('WebVuCoolOldBulb-2.jpg', 276, 110, 'Light Bulb');">
    Add Lightbulb
  </button> 

</form>
<script>
//https://stackoverflow.com/questions/25487865/html5-div-hidden-show-on-click
function show_image(src, width, height, alt) {
  var img = document.createElement("img");
  img.src = src;
  img.width = width;
  img.height = height;
  img.alt = alt;

  // This next line will just add it to the <body> tag
  document.body.appendChild(img);
}
</script>
</body>
</html>
4
  • 1
    Possible duplicate of HTML button to NOT submit form Commented Jun 21, 2016 at 18:39
  • ^ The problem is that your button is submitting the form. That question should help. Commented Jun 21, 2016 at 18:40
  • Sorry Mike I missed your hint about that posting. I will re read the posting and it's follow ups Commented Jun 21, 2016 at 19:13
  • Yes! Now I get it! I will edit the posting and show the corrected version. Thank you. I hope I clicked the right up arrow on your answer. Commented Jun 21, 2016 at 19:19

1 Answer 1

1
  • Use type=button as default behavior of <button> is to submit the form and hence, page will be unloaded.
  • Pass reference of image element so that element could be accessed using DOM-api.

function show_image(id, src, width, height, alt) {
  var img = document.getElementById(id);
  img.src = src;
  img.width = width;
  img.height = height;
  img.alt = alt;
}
<img id="cactusPhoto" src="img/WebVuHedgehogCalicoCactusInBloomJoshuaTreeLaurelShimer.jpg" alt="Picture from Joshua Tree." height="100" width="100">

<button type="button" onclick="show_image('cactusPhoto','WebVuCoolOldBulb-2.jpg',276,110,'Light Bulb');">Add Lightbulb</button>

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

1 Comment

No but at the time that I commented your code did not work at all.

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.