0

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;
            }
Waqar Janjua
  • 6,113
  • 2
  • 26
  • 36
niknowj
  • 977
  • 4
  • 19
  • 37
  • make a var firstclick =true;outside from the function and in Dosomething add firstclick=false ; or same technique in codebehind let me know if it doesnt work – skhurams Jul 13 '12 at 12:14
  • after first click means first time it will work and then becomes disable ? – Waqar Janjua Jul 13 '12 at 12:22
  • @Steve, I will do, but all answers didn't helped me. thats why. – niknowj Jul 13 '12 at 12:22
  • @WaqarJanjua yes the same first i want to call the method then becomes disable the link. – niknowj Jul 13 '12 at 12:23
  • @niknowj, sorry, I'm coming into this conversation late - but if you return `true` from `DoSomething()` then is the page not going to navigate to the `href` page? – freefaller Jul 13 '12 at 13:21

3 Answers3

1

In your javascript you can set the attribute disabled="" at your element. (look here)

If, with disable, you mean to not let the browser go to the url, you have to call event.preventDefault() (look here)

Community
  • 1
  • 1
Iridio
  • 9,213
  • 4
  • 49
  • 71
0

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.

Waqar Janjua
  • 6,113
  • 2
  • 26
  • 36
0

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.)

freefaller
  • 19,368
  • 7
  • 57
  • 87