0

Possible Duplicate:
How to enable or disable an anchor using jQuery?

I have this:

<a onclick="javascript:btnSave_onclick();" class="button">Save</a>

Basically, the link is acting like a button.

However, there are some conditions when I need to make it disabled. How to do this?

Community
  • 1
  • 1
petko_stankoski
  • 10,459
  • 41
  • 127
  • 231
  • Give the conditions to an `if` statement in your `btnSave_onclick` function. –  Apr 07 '12 at 15:04
  • @amnotiam But then the link should be clicked. I want it not to be able to be clicked. – petko_stankoski Apr 07 '12 at 15:05
  • Only way to prevent a user from clicking a link is to hide or remove it. –  Apr 07 '12 at 15:06
  • 2
    Are you actually asking how to remove the onclick handler ? –  Apr 07 '12 at 15:08
  • 1
    @amnotiam Hmm I gues so. How to do it with code? – petko_stankoski Apr 07 '12 at 15:12
  • 2
    Assuming you have a reference to the element, you can simply do `el.onclick = null`, or `el.removeAttribute('onclick')` should also work. –  Apr 07 '12 at 15:14
  • See the first comment from Kshitij Mehta – mddw Apr 07 '12 at 15:14
  • @mdi: The link in the first comment doesn't provide a solution for what OP wants. `e.preventDefault()` won't prevent the `onclick`, and OP has stated in the comments that the function shouldn't run at all. –  Apr 07 '12 at 15:16
  • I was thinking of removing the inline JS and putting it in an external function, then using preventdefault. Or (but it's very ugly), a CSS transparent layer preventing the click. – mddw Apr 07 '12 at 15:20
  • @mdi: It's certainly an option to get rid of the inline handler, but the `preventDefault` still won't have any effect. That just prevents the `href` from being followed. A transparent layer is certainly valid, but I agree that it's a little ugly. –  Apr 07 '12 at 15:25
  • And maybe pointer-events: none in CSS ? Not sure about the browser support. – mddw Apr 07 '12 at 15:29
  • @mdi: Interesting, I hadn't heard of pointer-events, but according to [this MDN page](https://developer.mozilla.org/en/CSS/pointer-events) it's primarily meant for SVG, and isn't yet standardized under CSS. –  Apr 07 '12 at 15:32
  • Yes, and according to your link, it won't work on IE and Opera. Duh. – mddw Apr 07 '12 at 15:38

1 Answers1

-1

Any reason you're not just using a button instead? In that case you could just use

document.getElementById('button').disabled = true; 

You would have to set an id for the button also

cmwright
  • 3,406
  • 5
  • 26
  • 33
  • 1
    Why didn't you write this as a comment? It certainly doesn't provide a solution to the question "how to prevent LINK from clicking?". – petko_stankoski Apr 07 '12 at 15:06
  • This does not answer the OP's question. – K Mehta Apr 07 '12 at 15:06
  • 2
    It's still a very fair point. If it doesn't look or behave like a link, well don't make it a link. In this case, it seems like a button would have been more suitable. – Nix Apr 07 '12 at 15:12
  • Didn't mean to offend anyone here... If the anchor isn't pointing somewhere, it could be a button in which case dealing with disabling it in js is quite straightforward. – cmwright Apr 07 '12 at 20:53