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?
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.
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.
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. <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 (); })
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.
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.).
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.
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();
})
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.
.disabledLink.disabled {pointer-events:none;}
That should do it hope I helped!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With