tags:

views:

35

answers:

2

I use the below to disable my submit image when it is clicked to prevent double clicking:

<input class="real" name="freshness" value="" type="text" size="5" maxlength="6"/><a href="javascript:void(0)" onClick="submitComment('+[id]+'); this.onclick=null; return false;"><img class="submitcommentimg" id="submitcommentimg<?php echo $id; ?>" src="/images/check.png" alt="Comment!" border="0"></a>

<a href="javascript:void(0)" onClick="submitComment('+[id]+'); this.onclick=null; return false;">

How would I re-enable this when my form returns a validation error?

A: 

Before setting the onclick function to null, we save the function in a property of the tag:

<a id="myLink" href="javascript:void(0)" onClick="submitComment('+[id]+'); this._onclick=this.onclick; this.onclick=null; return false;">

then if something goes wrong, we can just restore it:

document.getElementById("myLink").onclick = document.getElementById("myLink")._onclick;
Victor
How does that work...
ian
I have edited the answer, trying to be more clear.
Victor
+1  A: 

My suggestion:

var enableClick = true;

function doClick() {
  if(!enableClick)
    return false;
  submitComment('+[id]+');
  enableClick = false;
  return false;
}

<a onClick="doClick()">

You can later enable clicking again by setting enableClick = true;

Kaivosukeltaja