Results 1 to 3 of 3

Thread: onClick :-(

  1. #1
    Join Date
    Jun 2012

    onClick :-(

    hi all

    i have a small question. maybe very simple but i am new to this
    i tried to change a button which is image, with an onClick like:
    <img src="image1.jpg" id="image1" onclick="this.src='image2.jpg';" alt="image" />

    now it works perfect, but how do i change it back? what i would like to do is to change it once, when i click it one time, which is working, but when i click it the next time, it should go back to the image2.jpg again

    any help will be highly apreciated

    thanks in advance

  2. #2
    Join Date
    Mar 2011
    You need a little more code to toggle the image back and forth. Something like:
    <script type="text/javascript">
      function toggleImage(myImg) {
        myImg.src = (myImg.src == 'image1.jpg') ? 'image2.jpg' : 'image1.jpg';
      } // end toggleImg()
    <img src="image1.jpg" id="image1" onclick="toggleImage(this);" alt="image" />
    Rick Trethewey
    Rainbo Design

  3. #3
    Join Date
    May 2006
    Somewhere behind your screen
    if you need it with more than one image take a look at this page and see the inline comments
    signature under construction

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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