Assume that I have some HTML elements like these :
<div id="content_div">
<span><a href="some_link">Click me</a></span>
<div> Hello everybody. Click <a href="some_link_else">me</a> to do something else</div>
<a href="3rd_link"> Oops </a>
</div>
All that I need is get all "a" tags in the #content_div and disable all of them (I don't want user to click on them). How could I do that in Jquery?
Nope! Divs can't be disabled. Only form and form elems. Actually in Quirks-mode IE can disable a div , but it only grays button texts, it doesn't prevent events firing.
If you have a div, and you want to support click or a key event on that div, then you have to do two things: 1) When you want to disable the div, set its disabled attribute as usual (just to comply with the convention) 2) In your div's click and/or key handlers, check if disabled attribute is set on the div.
It is still possible to disable a link by following 3 steps: remove the href attribute so that it can no longer receive the focus. add a role="link" so that it is always considered a link by screen readers. add an attribute aria-disabled="true" so that it is indicated as being disabled.
I would rely less on jQuery as it might be disabled by the user, so if you want a CSS solution, you can do it like
#content_div {
pointer-events: none;
cursor: default;
}
Demo
Edit: To be precise, use this declaration #content_div a
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