Results 1 to 4 of 4

Thread: Two instances with OnClick

  1. #1
    Join Date
    Feb 2008
    Location
    Buenos Aires, Argentina
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Two instances with OnClick

    Hey people!
    I'm new at javascript and i'm really stuck with something that (I think) should be an easy thing to do. I'll explain:
    I have an image, let's call it IMAGE_1. When i do a click on it, it should change to IMAGE_2, but when i click on it again, it should change to IMAGE_1 again and so on...
    I tought it was easy, but my knowledge in javascript is not enough to finish it... I've used logic so far, but no luck, here's the code:

    Code:
    <img src='/imagenes/turnos/IMAGE_1.gif' width='10' height='10' name='disponible' id='disponible' border='0' onClick="javascript:if(this.src='/imagenes/turnos/IMAGE_1.gif'){this.src='/imagenes/turnos/IMAGE_2.gif';}else{this.src='/imagenes/turnos/IMAGE_1.gif';}">
    The first part works great, IMAGE_1 changes to IMAGE_2 on the first click, but i can't make it work to change to IMAGE_1 when I click on it again.

    It's not working after 2 hours of testing all sort of things... any ideas? Thanks in advance!

  2. #2
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,920
    Thanks
    2
    Thanked 267 Times in 262 Posts

    Default

    Hi there dsagrera,

    and a warm welcome to these forums.

    Have a look at this example, you should be able to adapt it to your requirements...
    Code:
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <base href="http://mysite.orange.co.uk/coothead/pop_up_gallery/">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    <style type="text/css">
    * {
        margin:0;
        padding:0;
     }
    #container {
        text-align:center;
     }
    </style>
    
    <script type="text/javascript">
    window.onload = function() {
     document.getElementById('pic').onclick=function() {
     this.src=(this.src.indexOf('ball_shad.jpg') != -1)?'ball_shad_thumb.jpg':'ball_shad.jpg';
      }	
     }
    </script>
    
    </head>
    <body>
    
    <div id="container">
    <img id="pic" class="thumb" src="ball_shad_thumb.jpg" alt="">
    </div>
    
    </body>
    </html>
    
    coothead

  3. The Following User Says Thank You to coothead For This Useful Post:

    dsagrera (02-24-2008)

  4. #3
    Join Date
    Feb 2008
    Location
    Buenos Aires, Argentina
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Talking Fixed!

    Ok, thanks coothead! by posting that example I finally fixed my code, and that function (IndexOf) helped me also to fix another problem I had with another script, so was 1 answer, 2 fixes! And that's great!

    Here's the fixed code for anyone who wants to use it:

    Code:
    <img src='/imagenes/turnos/turno_disponible.gif' width='10' height='10' name='disponible' id='disponible' border='0' onClick="if(this.src.indexOf('turno_disponible.gif') < 0){this.src='/imagenes/turnos/turno_disponible.gif';}else{this.src='/imagenes/turnos/turno_reservado.gif';}">
    The script starts with the light off ('image_1') turns on a red light ('image_2') with 1 click and turns it off on the second click ('image_1'). This also puts some information on a hidden text field and removes it if the light is off.

    I thought of this script to make an update of several database records, based on that on "lights". The ones turned on will be updated, while the others will be left alone.

    Again, thanks CootHead for your unvaluable help! Best regards.

  5. #4
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,920
    Thanks
    2
    Thanked 267 Times in 262 Posts

    Default

    No problem, you're welcome.

Bookmarks

Posting Permissions

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