1

Is there anyway to inhibit the click handler on a tags in HTML without the help of jQuery/Javascript?

disabled only works on button and input, but I've chosen to use a tags in my code. I suppose I can use span and simulate the exact a styles I currently have, but it would be nice if there's a simple way around this.

Thanks in advance.

Anriëtte Myburgh
  • 13,347
  • 11
  • 51
  • 72

3 Answers3

2

there are many ways to do that..

1.simplest:

a.onclick=function(){
 return false
}

2.standard/modern:

a.addEventListener('click',function(e){
 e.preventDefault()
},false)

example with class=active to activate the link.

http://jsfiddle.net/y7vVq/

cocco
  • 16,442
  • 7
  • 62
  • 77
2

Use # as href or omit the href. The link won't work anymore. As cocco noted, setting href to # has the disadvantage, that the history gets messed up and the browser jumps back to the top of the page. Nevertheless, the value # has been used on numerous pages.

Disabling the onClick is not possible, but you can use onclick="return false". This way nothing happens on click.

There is also a partial solution using css (described in this already answered question).

Community
  • 1
  • 1
urzeit
  • 2,863
  • 20
  • 36
  • 1
    hello is better than # as it also prevents from changing the hash. – cocco Sep 11 '13 at 09:49
  • @cocco: Which way of changing does it prevent? By javascript it's possible to set the attribute via `.setAttribute('href', 'http://something');` as well, so I see no advantage. – urzeit Sep 11 '13 at 10:20
  • i'm talking about the hashtag '#'. you don'teven need to set the hashtag you can set it to nothing. el.setAttribute('href', '');el.href='';link==disabled. – cocco Sep 11 '13 at 10:23
  • @cocco: While it is absolutely correct, that omitting the attribute does the job, I don't understand what you meant with *prevents from changing the hash*. Did you mean that it prevents you from *accidently* changing the href? – urzeit Sep 11 '13 at 10:31
  • nothing special... 1.i don't like the hash tag on the url if it's not needed. 2.if i remember correctly sometimes if you scrolled down it jumps up to the top on some browsers. – cocco Sep 11 '13 at 10:35
  • oh yeah and it messses up history.<- biggest problem – cocco Sep 11 '13 at 10:52
  • @cocco: Okay, I wasn't aware of the history-problem, right. – urzeit Sep 11 '13 at 11:44
0
onclick="return false"

This is the simple way to disable click in a tag

Love Trivedi
  • 3,899
  • 3
  • 20
  • 26