Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    how to make images disappear upon click

    This is my code so far:

    for (var i = 0; i < 10; i ++){
    if (i % 3 == 0){
    document.write("<br>")
    }
    document.write("<A HREF="#" onClick= "document.images[" + i + "].name ='blank.jpg';">")
    if (chips[i] == 10){
    document.write("<img src='1.gif' width = 100 height = 100 name =" + i + ">")
    }
    else{
    document.write("<img src='" + chips[i] + ".gif' width = 100 height = 100 name =" + i + "></a>")
    }
    }

    basically what i want is when users click the image, the images goes blank or gets removed or something. I have no idea how to do it.

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Set the display to none.

    onclick="this.style.display = 'none';"


    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • Users who have thanked A1ien51 for this post:

    firebat (01-22-2008)

  • #3
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by A1ien51 View Post
    Set the display to none.

    onclick="this.style.display = 'none';"


    Eric
    document.write("<A HREF="#" onClick= "document.images[" + i + "].display ='none';">")
    like this correct?

    edit: fixed the images not loading on the page problem, but it still won't disappear when i click it.
    Last edited by firebat; 01-22-2008 at 01:11 AM.

  • #4
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    this is all you need. You don't need to put an <a> around it

    document.write("<img src='images/frog.jpg' onclick='this.style.display=\"none\";'>");


    document.write("<A HREF="#" onclick= "document.images[" + i + "].display ='none';">")
    like this correct?
    it should be:
    document.write("<A HREF="#" onclick= "document.images[" + i + "].style.display ='none';">")
    if you really want to do it that way. It is easier to just add an onclick to the image itself

    if you need to dynamically add 'onclick' to all the images, you can do this:
    Code:
    window.onload = function() {
    for(var i = 0; i < document.images.length; i++)
       {
          document.images[i].setAttribute('onclick','this.style.display="none";');
       }
    }
    Last edited by thaspius; 01-22-2008 at 02:06 PM.

  • #5
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by firebat View Post
    document.write("<A HREF="#" onClick= "document.images[" + i + "].display ='none';">")
    like this correct?
    Quote Originally Posted by A1ien51 View Post
    Set the display to none.
    onclick="this.style.display = 'none';"
    display is a property of the objects style and not a property of the object itself
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #6
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    making an image disappear when you click it

    Quote Originally Posted by shyam View Post
    display is a property of the objects style and not a property of the object itself
    So I have an image with another image on top of it and what I want it to do is disappear when you click it. This is what I have so far:

    <!doctype html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Untitled Document</title>
    <link href="hopefully the final one /newstyle.css" rel="stylesheet" type="text/css">

    </head>

    <body>

    <div class="image-container">
    <div class="image-container"></div>
    <img src="1.jpg" alt="" width="686" height="539" usemap="#Map"/>


    <img class="star-button" src="2 .png" width="19" height="8" alt=""/></div>

    </body>
    </html>

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,574
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    That code won't put one image on top of the other. It will put the images side by side if they fit, else it will put the 2nd image under the first.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,574
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    And don't hijack a thread that is SIX YEARS OLD.

    Start a NEW thread with your own question fully described.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •