How to disable HTML links using JavaScript / jQuery ?
Last Updated :
19 Jan, 2023
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:
- attrName: This parameter is required. It specifies the name of the attribute to add.
- attrValue: This parameter is required. It specifies the value of the attribute to add.
Example 1: This example adds the class disabled to the <a> element with the help of setAttribute() method.
html
<!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:
- This method accepts single parameter attrName which is required. It specifies the name of the created attribute.
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)
- Parameters: This method accepts single parameter attributeNode which is required. It specifies the attribute node to be added.
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.
html
<!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:
- property: This parameter specifies the name of the property.
- value: This parameter specifies the value of the property.
- function(index,currentvalue): This parameter specifies a function that returns the property value to set.
- index: This parameter receives the index position of an element in the set.
- currentValue: This parameter receives the current property value of selected elements.
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:
- className: This parameter is required. It specifies one or more than one class name to add.
- function(index,currentClass): This parameter is optional. It specifies a function that returns one or more class names to add.
- index: It returns the index position of the element in the set.
- className: It returns the current class name of the selected element.
Example 1: This example adds the class('disabled') to the <a> element with the help of addClass() method.
html
<!DOCTYPE HTML>
<html>
<head>
<title>
How to disable HTML links
using jQuery
</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">
</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.
html
<!DOCTYPE HTML>
<html>
<head>
<title>
How to disable HTML links
using jQuery
</title>
<script src =
"https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">
</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 ?
Similar Reads
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Introduction to JavaScript JavaScript is a versatile, dynamically typed programming language used for interactive web applications, supporting both client-side and server-side development, and integrating seamlessly with HTML, CSS, and a rich standard library.JavaScript is a single-threaded language that executes one task at
7 min read
JSON Web Token (JWT) A JSON Web Token (JWT) is a standard used to securely transmit information between a client (like a frontend application) and a server (the backend). It is commonly used to verify users' identities, authenticate them, and ensure safe communication between the two. JWTs are mainly used in web apps an
7 min read
Frontend Developer Interview Questions and Answers Frontend development is an important part of web applications, and it is used to build dynamic and user-friendly web applications with an interactive user interface (UI). Many companies are hiring skilled Frontend developers with expertise in HTML, CSS, JavaScript, and modern frameworks and librarie
15+ min read
JavaScript Coding Questions and Answers JavaScript is the most commonly used interpreted, and scripted Programming language. It is used to make web pages, mobile applications, web servers, and other platforms. Developed in 1995 by Brendan Eich. Developers should have a solid command over this because many job roles need proficiency in Jav
15+ min read
Top 95+ Javascript Projects For 2025 JavaScript is a lightweight, cross-platform programming language that powers dynamic and interactive web content. From real-time updates to interactive maps and animations, JavaScript brings web pages to life.Here, we provided 95+ JavaScript projects with source code and ideas to provide hands-on ex
4 min read
Functions in JavaScript Functions in JavaScript are reusable blocks of code designed to perform specific tasks. They allow you to organize, reuse, and modularize code. It can take inputs, perform actions, and return outputs.JavaScriptfunction sum(x, y) { return x + y; } console.log(sum(6, 9)); // output: 15Function Syntax
5 min read
JavaScript Exercises, Practice Questions and Solutions JavaScript Exercise covers interactive quizzes, tracks progress, and enhances coding skills with our engaging portal. Ideal for beginners and experienced developers, Level up your JavaScript proficiency at your own pace. Start coding now! A step-by-step JavaScript practice guide for beginner to adva
3 min read
HTML DOM (Document Object Model) The HTML DOM (Document Object Model) is a programming interface that represents the structure of a web page in a way that programming languages like JavaScript can understand and manipulate. Think of it as a tree of objects where each part of your HTML document (elements, attributes, text) is repres
6 min read