Search code examples
javascriptphpdisable-link

Disable a href links in php-javascript


I have 4 links in my page(.phtml file) as follows

<ul id="fileMenu" class="contextMenu">
    <li class="add"><a id ="addbtn" href="#add" style="display:none;">Add</a></li>
    <li class="download"><a href="#download">Download</a></li>
    <li class="rename"><a href="#rename">Rename</a></li>
    <li class="del"><a href="#delete">Delete</a></li>
    <li class="copypath"><a href="#copypath">Copypath</a></li>
</ul>

I want to disable the add,rename and delete links Just disable it from UI .

User should be able to see it but no click event should be performed(linksshould be grayed out) .

I have used

  1. disable="disabled"

  2. display =none;

but they are not serving the purpose.

Any other way this would work out ?


Solution

  • I would use this CSS to visually disable a link:

    .disable {
        position: relative;
    }
    .disable:before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 100;
    }
    .disable a {
        color: gray;
        cursor: default;
    }
    

    Then in order to disable a link you would put a class disable on corresponding li:

    <li class="download disable"><a href="#download">Download</a></li>
    

    What it does is basically overlays a link with transparent pseudo element :before.

    Another solution could be using pointer-events: none rule but it's not well supported in IE.

    Demo: http://jsfiddle.net/LPz2Z/1/