Onclick image dissapear.

Discussion in 'JavaScript' started by popemobile, Nov 10, 2011.

  1. #1
    Hi, I need to make a picture dissapear onlick using java script. I will be honest I am lost because I'm new to java and I can't find a tutorial for this! I have been experimenting with other onclick uses (alerts etc) but this one is bugging me!

    Thanks,

    Pope
     
    popemobile, Nov 10, 2011 IP
  2. pr0t0n

    pr0t0n Well-Known Member

    Messages:
    243
    Likes Received:
    10
    Best Answers:
    10
    Trophy Points:
    128
    #2
    Add this to your image tag:
    
    onclick="this.style.display='none'; return false;"
    
    Code (markup):
    And if you want to have a nice pointer on the image as well, like if it was a link, then you can add this also:
    
    style="cursor: pointer;"
    
    Code (markup):
    So your whole image tag would look something like this:
    
    <img src="your-domain.com/your-photo-url.jpg" onclick="this.style.display='none'; return false;" style="cursor: pointer;" border="0" alt="" />
    
    Code (markup):
     
    pr0t0n, Nov 11, 2011 IP
  3. popemobile

    popemobile Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks alot! I had worked it out but only so that the picture left behind the little icon that shows it had failed to load!
     
    popemobile, Nov 11, 2011 IP
  4. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #4
    if you really want to remove the image by removing the img tag itseld onclick.. you can do this..


     <img src="image.png" onclick="this.parentNode.removeChild(this);" /> 
    HTML:
     
    JohnnySchultz, Nov 18, 2011 IP
  5. madmod001

    madmod001 Peon

    Messages:
    14
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    or you could just replace the image with a 1x1 clear gif if you need to use the img tag again
     
    madmod001, Dec 7, 2011 IP
  6. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #6
    Or you could put the img into a div:
    
    <div id="image"><img src="your-domain.com/your-photo-url.jpg" onclick="hideThis();" /></div>
    
    Code (markup):
    and run Javascript (not Java - that's an entirely different language) to hide the div itself (in the function hideThis()).
     
    Rukbat, Dec 10, 2011 IP