Disabled Href Tag - Javascript - Stack Overflow
Có thể bạn quan tâm
-
- Home
- Questions
- Tags
- Users
- Companies
- Labs
- Jobs
- Discussions
- Collectives
-
Communities for your favorite technologies. Explore all Collectives
- Teams
Ask questions, find answers and collaborate at work with Stack Overflow for Teams.
Try Teams for free Explore Teams - Teams
-
Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams
Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about CollectivesTeams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about TeamsGet early access and see previews of new features.
Learn more about Labs Disabled href tag [duplicate] Ask Question Asked 11 years, 11 months ago Modified 2 years, 10 months ago Viewed 1.1m times 420 This question already has answers here: How to disable HTML links (17 answers) Closed 2 years ago.Although that link is disabled, it's still clickable.
<a href="/" disabled="disabled">123n</a>Can I make it not-clickable if it's disabled? Should I use JavaScript necessarily?
Share Improve this question Follow asked Dec 19, 2012 at 15:29 Alan CoromanoAlan Coromano 26k55 gold badges139 silver badges214 bronze badges 6- 3 See stackoverflow.com/questions/2091168/disable-a-link-using-css – Fred Foo Commented Dec 19, 2012 at 15:31
- Just for clarification: I'm guessing what you mean is that you've disabled the link but the "click" event still fires? – Levi Hackwith Commented Dec 19, 2012 at 15:32
- 2 You can use the pointer-events:none; in css – ppsreejith Commented Dec 19, 2012 at 15:32
- 4 You can use preventDefault(); stackoverflow.com/questions/1357118/… – Ciack404 Commented Dec 19, 2012 at 15:33
- But why not just remove the href attribute? – MrBoJangles Commented Jul 10, 2019 at 17:13
30 Answers
Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 546With the help of css you will disable the hyperlink. Try the below
a.disabled { pointer-events: none; cursor: default; } <a href="link.html" class="disabled">Link</a>
Share Improve this answer Follow edited Mar 27, 2019 at 17:14 brk 50.3k10 gold badges58 silver badges82 bronze badges answered Nov 21, 2014 at 9:07 LakhanLakhan 13.2k3 gold badges20 silver badges29 bronze badges 11- 5 Please note that this works only in Internet Explorer 11+ – Patrick Hillert Commented Sep 17, 2015 at 8:13
- 63 When focus on link tag with tab key and press Enter this property doesn't work. – Majid Basirati Commented Apr 23, 2016 at 13:21
- 2 Should only be the answer if you don't care about IE9-10 – Ringo Commented Oct 27, 2016 at 21:19
- 3 also use opacity to differentiate between active and disabled links. opacity: 0.5; – Aamer Shahzad Commented Feb 18, 2019 at 18:29
- 4 @MajidBasirati I know it's an old post but I wanted to give an easy solution to the tab issue. You can always use tabindex="-1" inside your anchor tag in order to disable it. – Brayam Valero Commented May 6, 2021 at 21:09
There is no disabled attribute for hyperlinks. If you don't want something to be linked then you'll need to remove the <a> tag altogether.
Alternatively you can remove its href attribute - though this has other UX and Accessibility issues as noted in the comments below so is not recommended.
Share Improve this answer Follow edited Oct 30, 2020 at 15:25 Barry Pollard 45.4k8 gold badges85 silver badges99 bronze badges answered Dec 19, 2012 at 15:30 John CondeJohn Conde 220k99 gold badges461 silver badges501 bronze badges 6- 27 This works, but I would consider it bad UX if you leave the existing CSS on it. The user will click it and think something is broken. You should never allow the user to become confused about what is happening. – agm1984 Commented May 11, 2018 at 6:49
- Just adding cursor: none; to the relevant link seems to achieve as much as cursor: none; pointer-events: none; . . . . At any rate it's enabling a change of page in a single-page app site for me. – Trunk Commented Jan 21, 2019 at 20:51
- 2 The link would still be focusable and "clickable" (using Enter) using keyboard navigation even if you use CSS cursor or pointer-events, which is probably bad if you're trying to disable something Either remove the href attribute or turn it into a span – tbjgolden Commented Apr 13, 2019 at 22:50
- 5 You can also add onclick="return false;" to the anchor tag and perhaps add a title attribute that explains that the link is not valid. – Craig London Commented Jun 18, 2019 at 17:52
- 1 Also, I believe simply clearing out the href and leaving the <a> would not be ADA compliant. – SlothFriend Commented Oct 10, 2019 at 17:08
You can use:
<a href="/" onclick="return false;">123n</a> Share Improve this answer Follow answered Dec 19, 2012 at 15:31 CodeLikeBeakerCodeLikeBeaker 21.3k14 gold badges82 silver badges112 bronze badges 2- 7 will not work if there is already a function as a handler for click event – rahul shukla Commented Aug 28, 2019 at 5:29
- @rahulshukla There are catpure events which can catch events early. (javascript.info/bubbling-and-capturing) – janispritzkau Commented Jun 23, 2021 at 9:52
You can use one of these solutions:
HTML
<a>link</a>JavaScript
<a href="javascript:function() { return false; }">link</a> <a href="/" onclick="return false;">link</a>CSS
<a href="www.page.com" disabled="disabled">link</a> <style type="text/css"> a[disabled="disabled"] { pointer-events: none; } </style> Share Improve this answer Follow edited Aug 15, 2017 at 9:54 Blowsie 40.5k15 gold badges91 silver badges110 bronze badges answered Oct 8, 2014 at 15:05 IgniteCodersIgniteCoders 4,9703 gold badges46 silver badges64 bronze badges Add a comment | 47Try this:
<a href="javascript:void(0)" style="cursor: default;">123n</a> Share Improve this answer Follow answered Dec 19, 2012 at 15:32 user2742371user2742371 3- 1 Correct, I always use href="javascript:void(0)" – Jan Hamara Commented Nov 27, 2019 at 13:19
- 2 This is also a nice touch, if you you still want to retain the function of right click like "copy link" – Developer SPM Commented Apr 9, 2020 at 19:06
- 1 href='javascript:;' - that is enough already - href="javascript colon semicolon" – jumper rbk Commented Jun 17, 2023 at 17:30
The <a> tag doesn't have a disabled attribute, that's just for <input>s (and <select>s and <textarea>s).
To "disable" a link, you can remove its href attribute, or add a click handler that returns false.
Share Improve this answer Follow answered Dec 19, 2012 at 15:31 gen_Ericgen_Eric 227k42 gold badges302 silver badges342 bronze badges 2- @rocket Are there any implications of removing the href attribute? I mean, it's supposed to be a mandatory attribute. Not that that sort of thing seems to matter much nowadays. – Ringo Commented Oct 27, 2016 at 21:29
- @Ringo: Removing the href may make the cursor not change when you hover over it. – gen_Eric Commented Oct 27, 2016 at 21:35
HTML:
<a href="/" class="btn-disabled" disabled="disabled">123n</a>CSS:
.btn-disabled, .btn-disabled[disabled] { opacity: .4; cursor: default !important; pointer-events: none; } Share Improve this answer Follow answered Jan 26, 2016 at 10:32 SyedSyed 16.4k13 gold badges126 silver badges157 bronze badges 1- probably this is best for bootstrap – Abel Callejo Commented Aug 11, 2020 at 0:31
Tips 1: Using CSS pointer-events: none;
Tips 2: Using JavaScript javascript:void(0) (This is a best practice)
<a href="javascript:void(0)"></a>Tips 1: Using Jquery $('selector').attr("disabled","disabled");
Share Improve this answer Follow answered Mar 8, 2019 at 9:15 venkatskpivenkatskpi 8107 silver badges8 bronze badges Add a comment | 15You need to remove the <a> tag to get rid of this.
or try using :-
<a href="/" onclick="return false;">123n</a> Share Improve this answer Follow answered Dec 19, 2012 at 15:31 Rahul TripathiRahul Tripathi 172k32 gold badges289 silver badges339 bronze badges Add a comment | 7Letting a parent have pointer-events: none will disable the child a-tag like this (requires that the div covers the a and hasn't 0 width/height):
<div class="disabled"> <a href="/"></a> </div>where:
.disabled { pointer-events: none; } Share Improve this answer Follow edited Jun 14, 2017 at 19:36 answered Jun 14, 2017 at 18:46 FerusFerus 1,1083 gold badges12 silver badges19 bronze badges 4- You can also apply the "pointer-events:none" style directly to the <a> tag, for example by adding the .disabled class with javascript when you want to disable it. A wrapping <div> is not necessary. – Les Nightingill Commented Aug 5, 2017 at 14:45
- Wrap not required as demonstrated here: https://jsfiddle.net/d1owm1d0/1/ for chrome, firefox, and safari. Are you using a different browser where my demo fails? – Les Nightingill Commented Aug 5, 2017 at 18:13
- @LesNightingill If the a-tag wraps something it doesn't work: jsfiddle.net/d9gwgdyf – Ferus Commented Aug 5, 2017 at 18:21
- that's correct @Ferus. I'm not sure why that is true. – Les Nightingill Commented Aug 5, 2017 at 19:50
- 1 This Code is working on right click.So, its not an exact solution. – Lakhan Commented Feb 24, 2015 at 9:48
You can emulate the disabled attribute on a <a> tag.
<a href="link.html" disabled="">Link</a>
a[disabled] { pointer-events: none; cursor: default; } Share Improve this answer Follow answered Aug 15, 2017 at 9:20 BlowsieBlowsie 40.5k15 gold badges91 silver badges110 bronze badges Add a comment | 5you can disable link using javascript at run time by using this code
Share Improve this answer Follow answered May 22, 2018 at 13:29 Chintan MathukiyaChintan Mathukiya 3935 silver badges26 bronze badges 1$('.page-link').css("pointer-events", "none");
- Late to the party, but this also removes any cursor css. In my case, I use the "cursor: not-allowed" if a link is disabled. – Steve Commented Mar 22, 2019 at 10:33
I have one:
<a href="#">This is a disabled tag.</a>Hope it will help you ;)
Share Improve this answer Follow answered Jan 24, 2020 at 20:32 user9861020user9861020 3- 1 Hi Werenverlivitz, welcome. Nice, didn't think you'd be the first person with such answer 7-8 years after! – Tiago Peres Commented Jan 24, 2020 at 21:10
- It's not disabled, it just goes to the current page, possibly navigating away from current section. – siride Commented Feb 8, 2023 at 16:04
- Ts doesn't allow doing this: "The href attribute requires a valid value to be accessible" – misolo Commented Mar 27, 2023 at 10:40
If you want to get rid of the pointer you can do this with css using cursor.
Share Improve this answer Follow answered Dec 19, 2012 at 15:33 ahdanielsahdaniels 1,0601 gold badge12 silver badges25 bronze badges 0 Add a comment | 4In my case, I use
<a href="/" onClick={e => e.preventDefault()}> Share Improve this answer Follow answered Sep 11, 2017 at 8:34 Chien Chieh WangChien Chieh Wang 1511 silver badge3 bronze badges Add a comment | 4I was able to achieve the desired result using an ng-href ternary operation
<a ng-href="{{[condition] ? '' : '/'}}" ng-class="{'is-disabled':[condition]}">123n</a>where
a.is-disabled{ color: grey; cursor: default; &:hover { text-decoration: none; } } Share Improve this answer Follow answered Sep 20, 2019 at 12:46 Corey WebsterCorey Webster 412 bronze badges Add a comment | 4We can't disable it directly but we can do the following:
- add type="button".
- remove the href="" attribute.
- add disabled attribute so it shows that it's disabled by changing the cursor and it becomes dimmed.
example in PHP:
<?php if($status=="Approved"){ ?> <a type="button" class="btn btn-primary btn-xs" disabled> EDIT </a> <?php }else{ ?> <a href="index.php" class="btn btn-primary btn-xs"> EDIT </a> <?php } ?> Share Improve this answer Follow edited Jan 15, 2021 at 3:37 gekkedev 5934 silver badges20 bronze badges answered Apr 8, 2018 at 16:45 Assaad LutfAssaad Lutf 651 silver badge3 bronze badges 2- 2 The type attribute is for MIME type and there is no "button" type, see HTML <a> type Attribute. Also disabled is not an attribute of <a> – Bjarke Pjedsted Commented Oct 21, 2020 at 6:07
- This is not a valid solution – Turnip Commented Jul 30 at 7:55
The best answer is always the simplest. No need for any coding.
If the (a) anchor tag has no href attribute, it is only a placeholder for a hyperlink. Supported by all browsers!
<a href="/">free web counters</a><br /> <a "/">free web counters</a>
Share Improve this answer Follow edited May 24, 2020 at 1:28 answered May 24, 2020 at 0:56 IntergalacticIntergalactic 392 bronze badges Add a comment | 2Use buttons and links correctly.
• Buttons activate scripted functionality on a web page (dialogs, expand/collapse regions).
• Links take you to another location, either to a different page or different location on the same page.
If you follow these rules there won't be any scenario when you need to disable a link. Even if you make a link look visually disabled and blank onclick
<a href="" ng-click="Ctrl._isLinkActive && $ctrl.gotoMyAwesomePage()"
You won't be considering accessibility and screen readers will read it as a link and visually impaired people will keep wondering why is the link not working.
Share Improve this answer Follow answered Feb 7, 2018 at 6:00 AmitAmit 8334 silver badges10 bronze badges Add a comment | 2Here are various ways to disable the a tag :
<a >Link Text</a> remove the href from your anchor tag <a href=”javascript:void(0)”/>Link text</a> put javascript:void(0) in href <a href="/" onclick="return false;">Link text</a> using return false$( "a" ).click(function( e ) { e.preventDefault(); $( "<div>" ) .append( "default " + e.type + " prevented" ) .appendTo( "#log" ); });As there is no disabled attributes for anchor tag. If you want to stop anchor tag behavior inside a jQuery function than you can use the the below line in the start of the function :
event.preventDefault()
Share Improve this answer Follow answered Nov 20, 2019 at 10:40 Mahak ChoudharyMahak Choudhary 1,3741 gold badge16 silver badges14 bronze badges Add a comment | 2I see there are already a ton of answers posted here, but I don’t think there’s any clear one yet that combines the already mentioned approaches into the one I’ve found to work. This is to make the link both appear disabled, and also not redirect the user to another page.
This answer assumes you’re using jquery and bootstrap, and uses another property to temporarily store the href property while disabled.
//situation where link enable/disable should be toggled function toggle_links(enable) { if (enable) { $('.toggle-link') .removeClass('disabled') .prop('href', $(this).attr('data-href')) } else { $('.toggle-link') .addClass('disabled') .prop('data-href', $(this).prop('href')) .prop('href','#') } a.disabled { cursor: default; } Share Improve this answer Follow answered May 10, 2020 at 19:59 owengallowengall 4646 silver badges14 bronze badges Add a comment | 1If you're using WordPress, I created a plugin that can do this & much more without needing to know how to code anything. All you need to do is add the selector of the link(s) that you want to disable & then choose "Disable all links with this selector in a new tab." from the dropdown menu that appears and click update.
Click here to view a gif that demonstrates this
You can get the free version from the WordPress.org Plugin repository to try it out.
Share Improve this answer Follow edited May 25, 2019 at 2:58 Bhargav Rao 51.9k29 gold badges126 silver badges141 bronze badges answered May 24, 2019 at 17:04 Chris MarabateChris Marabate 194 bronze badges 0 Add a comment | 1If you need to disabled link on laravel with Javascript, here is my solution:
Link(blade.php):
<a href='/link/to/path' class='btn btn-primary mt-3' onclick='disableLink(this)'>Confirmar</a>.css file
.isDisabled { cursor: not-allowed; opacity: 0.5; } a[aria-disabled="true"] { color: currentColor; display: inline-block; /* For IE11/ MS Edge bug */ pointer-events: none; text-decoration: none; }.js file
function disableLink(link) { // 1. Add isDisabled class to parent element link.parentElement.classList.add('isDisabled'); // 2. Store href so we can add it later link.setAttribute('data-href', link.href); // 3. Set aria-disabled to 'true' link.setAttribute('aria-disabled', 'true'); } function enableLink(link) { // 1. Remove 'isDisabled' class from parent span link.parentElement.classList.remove('isDisabled'); // 2. Set href link.href = link.getAttribute('data-href'); // 3. Remove 'aria-disabled', better than setting to false link.removeAttribute('aria-disabled'); }Reference: https://css-tricks.com/how-to-disable-links/
Share Improve this answer Follow edited Jan 10, 2022 at 18:13 answered Jan 10, 2022 at 17:39 Eduardo SteffensEduardo Steffens 1331 silver badge7 bronze badges Add a comment | 0Anything in the href tag will display at the bottom-left of the browser window when you mouse over it.
I personally think having something like javascript:void(0) displayed to the user is hideous.
Instead, leave href off, do your magic with with jQuery/whatever else and add a style rule (if you still need it to look like a link):
a { cursor:pointer; } Share Improve this answer Follow answered Mar 12, 2015 at 9:46 Enigma PlusEnigma Plus 1,54822 silver badges34 bronze badges Add a comment | -1You can disable anchor tags by returning false. In my case Im using angular and ng-disabled for a Jquery accordion and I need to disable the sections.
So I created a little js snippet to fix this.
<a class="opener" data-toggle="collapse" data-parent="#shipping-detail" id="shipping-detail-section" href="#shipping-address" aria-expanded="true" ng-disabled="checkoutState.addressSec"> Shipping Address</a> <script> $("a.opener").on("click", function () { var disabled = $(this).attr("disabled"); if (disabled === 'disabled') { return false; } }); </script> Share Improve this answer Follow edited Nov 1, 2016 at 15:51 answered Nov 1, 2016 at 15:46 Darryl WindsorDarryl Windsor 1091 silver badge4 bronze badges Add a comment | -1if you just want to temporarily disable the link but leave the href there to enable later (which is what I wanted to do) I found that all browsers use the first href. Hence I was able to do:
<a class="ea-link" href="javascript:void(0)" href="/export/">Export</a> Share Improve this answer Follow answered Nov 25, 2017 at 0:15 Angus WalkerAngus Walker 3983 silver badges13 bronze badges 1- This is invalid HTML, as described at stackoverflow.com/q/26341507/1709587. If your motivation is just to keep the href around in your source code so it's easy to edit your source later to restore it, there are ways of doing that that don't involve violating the spec, like using a comment. I'd recommend against writing invalid HTML like this without a solid reason for doing so. – Mark Amery Commented Dec 8, 2018 at 15:40
Variant that suits me:
<script> function locker(){ if ($("a").hasClass("locked")) { $("a").removeClass('locked').addClass('unlocked'); $("a").attr("onClick","return false"); } else { $("a").css("onclick","true"); $("a").removeClass('unlocked').addClass('locked'); $("a").attr("onClick",""); } } </script> <button onclick="locker()">unlock</button> <a href="http://some.site.com" class="locked"> <div> .... <div> </a> Share Improve this answer Follow edited Jan 19, 2018 at 17:16 Lewis86 5116 silver badges15 bronze badges answered Mar 16, 2014 at 14:13 LitterLitter 1271 silver badge2 bronze badges Add a comment | -1<a href="/" disabled="true" onclick="return false">123</a>
Just adding: This works in general, however it wont work if user has disabled javascript in browser.
1) You could optionally use Bootstrap 3 class on your anchor tag to disable the href tag, after integrating bootstrap 3 plugin do
<a href="/" class="btn btn-primary disabled">123n</a>
Or
2) Learn how to enable javascript using html or js in browsers. or create a pop-up telling user to enable javascript using before using the website
Share Improve this answer Follow edited Mar 31, 2018 at 15:41 answered Mar 31, 2018 at 15:31 OlaJOlaJ 6887 silver badges15 bronze badges Add a comment | -3There is an easy and clean way like so:
<a href="/shopcart/"> <button class="btn" disabled> Make an Order </button> </a>Having disabled attribute on a <button> by default does not let clicks go through <button> element up to <a> element. So <a> element does not even know that some clicks happened. Manipulate by adding/removing disabled attribute on a <button>.
Share Improve this answer Follow edited Aug 10, 2020 at 21:28 answered Aug 10, 2020 at 8:56 MaxSemenovMaxSemenov 93 bronze badges 1- <a href="/shopcart/" class="btn"> Make an Order </a> – Adeel Ahmed Baloch Commented Sep 9, 2020 at 12:13
Not the answer you're looking for? Browse other questions tagged
or ask your own question.- The Overflow Blog
- Your docs are your infrastructure
- Featured on Meta
- More network sites to see advertising test [updated with phase 2]
- We’re (finally!) going to the cloud!
- Call for testers for an early access release of a Stack Overflow extension...
Linked
346 How to disable HTML links 1 How to disable redirecting for some elements in an anchor tag? 0 Disabled attribute for anchor tag not working -1 how to inactive selected URL from websites? 0 How to disable a link based on URL parameter in Struts2 if condition 3261 event.preventDefault() vs. return false 947 How to disable a link using only CSS 112 Disable a link in Bootstrap 90 Angular2, what is the correct way to disable an anchor element? 66 AngularJS - ng-disabled not working for Anchor tag See more linked questionsRelated
8514 What does "use strict" do in JavaScript, and what is the reasoning behind it? 6334 What is the difference between "let" and "var"? 7650 var functionName = function() {} vs function functionName() {} 3095 How to store objects in HTML5 localStorage/sessionStorage 4447 Which "href" value should I use for JavaScript links, "#" or "javascript:void(0)"? 2699 Can (a== 1 && a ==2 && a==3) ever evaluate to true? 3344 Why does my JavaScript code receive a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error, while Postman does not? 2396 How can I guarantee that my enums definition doesn't change in JavaScript?Hot Network Questions
- Map or Thread operation for list
- Suddenly lost my admin status
- How to identify unsafe trees for climbing stand?
- Deutsche Bahn Berlin: can I use a different departure station?
- Do I need Letter of invitation to Iceland?
- Why does Japanese not have a native "tu" sound?
- BSD Licensed code used as a part of a research project
- Arrange 3 red balls and 33 white balls randomly in a circle. What is the probability that there are no more than 13 consecutive white balls?
- Are prenups legally binding in England?
- Is ATL-98 Carvair still alive in the US?
- How is one supposed to play these notes?
- turn wire frame of ico sphere into a cage
- Sub panel location question
- How to Draw a Diagram with a Custom Coordinate System and Shaded Areas?
- What would T-Rex cavalry be used for?
- Is there a symbol for the Hyper key?
- Do we have an idiom saying a person talking too loudly or often screaming?
- BIOS drive order is inverse of Windows'
- A roulette wheel? An AC Milan kit? Darth Maul's face?
- How to find file names but only with grep .html OR .php?
- Skylab's Boom during EVA
- Are there any examples of exponential algorithms that use a polynomial-time algorithm for a special case as a subroutine (exponentially many times)?
- What mechanism could cause a person not to cast a reflection?
- Are mental images of mathematical entities persistent?
Từ khóa » Html A Tag Disabled
-
Disable A HTML Link/anchor Tag - Code With Hugo
-
HTML Input Disabled Attribute - W3Schools
-
HTML Disabled Attribute - W3Schools
-
How To Disable Links | CSS-Tricks
-
Disable HTML Elements - Orange Digital Accessibility Guidelines
-
How To Disable A HTML Anchor Tag
-
How To Disable A Anchor Tag In HTML? - NiceSnippets
-
HTML Attribute: Disabled - HTML: HyperText Markup Language | MDN
-
HTML Disabled Attribute - GeeksforGeeks
-
HTML | Disabled Attribute - GeeksforGeeks
-
How To Disable A Tag Code Example
-
HTMLSelectElement.disabled - Web APIs | MDN
-
Disabling A Link
-
HTML Button Disabled Attribute - Dofactory