How to disable anchor tag after first click in javascript ?
-
20-06-2021 - |
Question
I have a a href tag with css class thickbox I need to disable the link after first click. how I can do this?
Aspx code
<a href="SomePage.aspx?FDID=11&KeepThis=true&TB_iframe=true&height=150&width=400"
onclick="return DoSomething(this)" class="thickbox" id="AnchorID">
<img id="MyImageButton" alt="Image" src="SiteImages/image.png" runat="server" />
</a>
My JavaScript Method
function DoSomething(element) {
return true;
}
OTHER TIPS
For this you have to create a hidden feild which will store a value example 1 or 0. If the user clicks the link first time set the hidden feild value to 1 and allow the user to redirect and then disable the hyperlink. If user again clicks the hyper link then first check the hidden feild if it contains a value 1 then don't allow him to redirect. Hope you understand my point. Also set the href property in the javascript.
I'm not sure exactly what the OP is trying to do, but if it is purely to allow the script to run the once after clicking, something like this would work...
function DoSomething(element) {
if(element.getAttribute("block")==null){
...
element.setAttribute("block","1");
return true;
}else{
return false;
}
}
This has the advantage of not changing the style of button by setting the disabled
attribute.
However, returning true
from the function and then in turn returning true
to the OnClick
handler of the link will result in the browser navigating to the URL in href
... so I can't see why the need for the blocking of a 2nd run. (Unless thickbox
, which I know nothing about, works in a way which is not the normal.)