SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard boxhead's Avatar
    Join Date
    Feb 2004
    Location
    UK
    Posts
    1,040
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    disable image onclick

    I have an image which runs a script onclick. How do I disable this onclick once it has been used? The image is not in a form.

    monkey
    monkey - the rest is history

  2. #2
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can detach the event, or alternatively just add an extra variable idicating that the image has been clicked.

    Detach the event (IE Code):
    Code:
    function clickImg() {
      var oImg = event.srcElement;
      ...
      oImg.detachEvent("onclick",clickImg);
    }
    Alternative:
    Code:
    var bImgClicked = false;
    function clickImg() {
      var oImg = event.srcElement;
      if (!bImgClicked) {
    	...
    	bImgClicked = true;
      }
    }
    Hope that answers your question?
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  3. #3
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    simplest possible:

    <img onclick="doTheJob(); this.onclick=null" ...

  4. #4
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm... I'd have to go with stereofrog on this one... that really does keep it simple.

    Nice one dude.
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  5. #5
    SitePoint Wizard boxhead's Avatar
    Join Date
    Feb 2004
    Location
    UK
    Posts
    1,040
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that'll do the job

    cheers both
    monkey - the rest is history

  6. #6
    SitePoint Addict Guimauve's Avatar
    Join Date
    Aug 2004
    Location
    Chicago
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dang, I never thought of removing the event handler code. Good one.

    Charles


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
  •