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.

4
  • Javascript? Try onclick="return false;" ... but it's not very pretty. Do you use the "href" Tag? Commented Sep 11, 2013 at 9:34
  • 3
    Use a span instead of an anchor like you propose. If you don't want something to behave like an anchor, then don't make it an anchor. Commented Sep 11, 2013 at 9:35
  • Do you need to have "href" set correctly to some page? Commented Sep 11, 2013 at 9:35
  • Try This .disabled { pointer-events: none; cursor: default; } <a href="#" class="disabled">link</a> Commented Apr 12, 2017 at 10:11

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

7 Comments

<a>hello</a> is better than # as it also prevents from changing the hash.
@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.
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='';<a>link</a>==disabled.
@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?
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.
|
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/

6 Comments

I assume there's also no way to do it without Javascript also?
no.. that is not possible.
Yes it is possible using CSS
not in a proper way.. but we can never be shure... so demo on jsfiddle?
also putting a # inside the href is a solution (btw you need js to change that to) but that moves the page to the top if iremember correctly. or using css3 disabling the click but thats not supported on older browsers.
|
0
onclick="return false" 

This is the simple way to disable click in a tag

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.