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

You can detach the event, or alternatively just add an extra variable idicating that the image has been clicked.

Detach the event (IE Code):


function clickImg() {
  var oImg = event.srcElement;
  ...
  oImg.detachEvent("onclick",clickImg);
}

Alternative:


var bImgClicked = false;
function clickImg() {
  var oImg = event.srcElement;
  if (!bImgClicked) {
	...
	bImgClicked = true;
  }
}

Hope that answers your question?

simplest possible:

<img onclick=“doTheJob(); this.onclick=null” …

Hmmm… I’d have to go with stereofrog on this one… that really does keep it simple.

Nice one dude. :wink:

that’ll do the job

cheers both :smiley:

Dang, I never thought of removing the event handler code. Good one.

Charles