SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    onclick in a tag can't work with if statement

    I don't understand why the image swapping doesn't work when I click the 2nd time:
    Code HTML4Strict:
    <a href="JavaScript:;" onclick="if(document.getElementById('img-book_1').src!=='images/minimize.jpg'){document.getElementById('img-book_1').src='images/minimize.jpg';}else{document.getElementById('img-book_1').src='images/maximize.jpg';}"><img id="img-book_1" src="images/maximize.jpg" style="float: right;border: none;" /></a>
    On the first click it swaps to the 2nd image but on the 2nd click it doesn't swap back to the first.
    Compare bible texts (and other tools):
    TheWheelofGod

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    src always contains the full URL regardless of what is assigned to it. Try:
    Code:
    <a href="#" onclick = "var img = document.getElementById('img-book_1'); img.src = ( img.src.search('minimize.jpg') > -1 ? 'images/maximize.jpg' : 'images/minimize.jpg' ); return false;">
     <img id="img-book_1" src="images/maximize.jpg" style="float: right;border: none;" />
    </a>
    Tab-indentation is a crime against humanity.


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
  •