ian
ian

Reputation: 12345

enabling a disabled submital image with javascript

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?

Upvotes: 0

Views: 102

Answers (2)

Kaivosukeltaja
Kaivosukeltaja

Reputation: 15735

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;

Upvotes: 1

Victor
Victor

Reputation: 9269

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;

Upvotes: 0

Related Questions