Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all link in a div element and disable them all

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?

like image 334
Nấm Lùn Avatar asked Jun 19 '13 08:06

Nấm Lùn


People also ask

Can you disable an entire div?

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.

How do you make a div tag disabled?

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.

How do I disable a link in HTML?

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.


1 Answers

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

like image 89
Mr. Alien Avatar answered Sep 28 '22 12:09

Mr. Alien