Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html- how to disable <a href>? [duplicate]

Tags:

html

href

I created a button that open a modal window on click.

<a href="#"  data-toggle="modal" data-target="#myModal" class="signup-button gray-btn pl-pr-36" id="connectBtn"  data-role="disabled">Connect</a>

For some reason the data-role="disabled" doesn't work good. How can I disable it?

like image 562
sarit rotshild Avatar asked Feb 04 '15 09:02

sarit rotshild


People also ask

How do I turn off anchor links?

The best way to disable a link tag is by using the CSS property pointer-events: none; . When you apply pointer-events: none; to any element, all click events will be disabled. Because it applies to any element, you can use it to disable an anchor tag. By using a CSS class, you can disable multiple anchor tags at once.

How can I disable href If onclick is executed?

To disable href if onclick is executed with JavaScript, we call preventDefault to stop the default navigation action. document. getElementsById("ignore-click"). addEventListener("click", (event) => { event.

How do you disable a link in CSS?

Answer: Use the CSS pointer-events Property You can simply use the CSS pointer-events property to disable a link. The none value of this property specify the element is never the target of pointer events.

How to disable href for a link in HTML?

How to disable href for a link. <style> .disabled { pointer-events: none; cursor: default; } </style> <a href="somelink.html" class="disabled">Some link</a> You can also use JavaScript. $ ('.disabled').click (function (e) { e.preventDefault (); })

How to disable the <a> tag in HTML?

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.

What is the use of disabled in HTML?

Definition and Usage. The disabled attribute is a boolean attribute. When present, it specifies that the element should be disabled. A disabled element is unusable. The disabled attribute can be set to keep a user from using the element until some other condition has been met (like selecting a checkbox, etc.).

How to disable a button in a HTML page?

We can't disable it directly but we can do the following: 1 add type="button". 2 remove the href="" attribute. 3 add disabled attribute so it shows that it's disabled by changing the cursor and it becomes dimmed.


3 Answers

You can use CSS to accomplish this:

.disabled {
  pointer-events: none;
  cursor: default;
}
<a href="somelink.html" class="disabled">Some link</a>

Or you can use JavaScript to prevent the default action like this:

$('.disabled').click(function(e){
    e.preventDefault();
})
like image 197
iivannov Avatar answered Oct 02 '22 03:10

iivannov


I created a button...

This is where you've gone wrong. You haven't created a button, you've created an anchor element. If you had used a button element instead, you wouldn't have this problem:

<button type="button" data-toggle="modal" data-target="#myModal" data-role="disabled">
    Connect
</button>

If you are going to continue using an a element instead, at the very least you should give it a role attribute set to "button" and drop the href attribute altogether:

<a role="button" ...>

Once you've done that you can introduce a piece of JavaScript which calls event.preventDefault() - here with event being your click event.

like image 34
James Donnelly Avatar answered Oct 02 '22 01:10

James Donnelly


.disabledLink.disabled {pointer-events:none;}

That should do it hope I helped!

like image 45
Grant Hood Avatar answered Oct 02 '22 03:10

Grant Hood