Open In App

How to disable HTML links using JavaScript / jQuery ?

Given an HTML link and the task is to disable the link by using JavaScript/jQuery.

Approach 1: Disable HTML link using JavaScript using setAttribute() Method.



JavaScript setAttribute() Method: This method adds the defined attribute to an element and gives it to the passed value. In case of the specified attribute already exists, the value is set/changed.

Syntax:



element.setAttribute(attrName, attrValue)

Parameters:

Example 1: This example adds the class disabled to the <a> element with the help of setAttribute() method.




<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        How to disable HTML links
        using JavaScript
    </title>
  
    <style>
        a.disabled {
            pointer-events: none;
        }
    </style>
</head>
  
<body>
  
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
  
    <a href="#" id="GFG_UP">
        LINK
    </a>
  
    <br><br>
  
    <button onclick="gfg_Run()">
        disable
    </button>
  
    <p id="GFG_DOWN" style="color:green; 
          font-size: 20px; font-weight: bold;">
    </p>
  
    <script>
        var link = document.getElementById('GFG_UP');
        var down = document.getElementById('GFG_DOWN');
          
        function gfg_Run() {
        link.setAttribute("class", "disabled");
        link.setAttribute("style", "color: black;");
        down.innerHTML = "Link disabled";
        }        
    </script>
</body>
  
</html>

Output:

How to disable HTML links using JavaScript / jQuery ?

Approach 2: Disable HTML link using JavaScript using the setAttributeNode() and createAttribute() method.

JavaScript createAttribute() Method: This method creates an attribute with the defined name and returns the attribute as an attribute object.

Syntax:

document.createAttribute(attrName)

Parameters:

Return value: It returns a node object, denoting the created attribute.

JavaScript setAttributeNode() Method: This method adds the specified attribute node to an element. In case of the specified attribute already exists, this method replaces it.

Syntax:

element.setAttributeNode(attributeNode)

Return Value: This method returns an attribute object which represents the replaced attribute node otherwise it returns null.

Example : This example adds the class disable to the <a> element with the help of setAttributeNode() method by first creating an attribute using createAttribute() method and then adding it to the <a> element.




<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        How to disable HTML links
        using JavaScript
    </title>
  
    <style>
        a.disabled {
            pointer-events: none;
        }
    </style>
</head>
  
<body>
  
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
  
    <a href="#" id="GFG_UP">
        LINK
    </a>
  
    <br><br>
  
    <button onclick="gfg_Run()">
        disable
    </button>
  
    <p id="GFG_DOWN" style="color:green; 
          font-size: 20px; font-weight: bold;">
    </p>
  
    <script>
        var link = document.getElementById('GFG_UP');
        var down = document.getElementById('GFG_DOWN');
          
        function gfg_Run() {
            var attr = document.createAttribute("class");
            attr.value = "disabled";                        
            link.setAttributeNode(attr);
            link.setAttribute("style", "color: black;");
            down.innerHTML = "Link disabled";
        }        
    </script>
</body>
  
</html>

Output:

How to disable HTML links using JavaScript / jQuery ?

Disable HTML link using jQuery

jQuery prop() Method: This method set/return properties and values of the matched elements. If this method is used to return the property value, it returns the value of the first selected element. If this method is used to set property values, it sets one or more property/value pairs for the set of selected elements.

Syntax:

$(selector).prop(property) // Return the value of an property 
$(selector).prop(property,value) // Set the property and value

// Set property and value using a function 
$(selector).prop(property,function(index,currentvalue))  
// Set multiple properties and values
$(selector).prop({property:value, property:value,...})  

Parameters:

addClass() Method: This method adds one or more than one class name to the specified elements. This method does not do anything with existing class attributes, it adds one or more than one class name to the class attribute.

Syntax:

$(selector).addClass(className,function(index,currentClass))

Parameters:

Example 1: This example adds the class(‘disabled’) to the <a> element with the help of addClass() method.




<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        How to disable HTML links
        using jQuery
    </title>
  
    </script>
  
    <style>
        a.disabled {
            pointer-events: none;
        }
    </style>
</head>
  
<body>
  
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
  
    <a href="#" id="GFG_UP">
        LINK
    </a>
  
    <br><br>
  
    <button onclick="gfg_Run()">
        disable
    </button>
  
    <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;">
    </p>
  
    <script>
        function gfg_Run() {
            $('a').addClass("disabled");
            $('a').css('color', 'black');
            $('#GFG_DOWN').text("Link disabled");
        }        
    </script>
</body>
  
</html>

Output:

How to disable HTML links using JavaScript / jQuery ?How to disable HTML links using JavaScript / jQuery ?

Example 2: This example adds the class(‘disabled’) to the <a> element with the help of prop() method.




<!DOCTYPE HTML>
<html>
    <head>
        <title>
            How to disable HTML links
            using jQuery
        </title>
          
        <script src =
        </script>
          
        <style>
            a.disabled {
                pointer-events: none;
            }
        </style>
    </head>
      
    <body>
      
        <h1 style = "color:green;" >
            GeeksForGeeks
        </h1>
  
        <a href = "#" id = "GFG_UP">
            LINK
        </a>
          
        <br><br>
          
        <button onclick = "gfg_Run()">
            disable
        </button>
          
        <p id = "GFG_DOWN" style =
            "color:green; font-size: 20px; font-weight: bold;">
        </p>
          
        <script>
            function gfg_Run() {
                $('a').prop("class","disabled");
                $('a').css('color', 'black');
                $('#GFG_DOWN').text("Link disabled");
            }        
        </script>
    </body>
</html>

Output:

How to disable HTML links using JavaScript / jQuery ?


Article Tags :