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
How to Disable Textarea using JavaScript?
This article will show you how to disable the textarea using JavaScript. Disabling a textarea using JavaScript can be done by setting the disabled property of the textarea element to true. This prevents the user from editing the content of the textarea. There are some reasons to use disabled textare
2 min read
How to disable radio button using JavaScript ?
Radio buttons let users select one option from many, commonly used in forms. Sometimes, based on a user's previous choice, certain options need to be disabled. For example, if a user selects "No" to playing games, we can disable related game options. Using JavaScript, about 65% of forms use such log
4 min read
How to remove HTML tags from a string using JavaScript ?
Removing HTML tags from a string in JavaScript means stripping out the markup elements, leaving only the plain text content. This is typically done to sanitize user input or to extract readable text from HTML code, ensuring no unwanted tags remain in the string.HTML tags come in two forms: opening t
3 min read
How to disable ALT key using jQuery ?
jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers What is the use of it ? Disabling ALT keys have a wide range
2 min read
JavaScript - Remove the href Attribute from Links Using jQuery or JS
Removing the href attribute from links using jQuery is a common task when you want to disable navigation functionality temporarily or modify link behavior dynamically. Here's how you can achieve this with simple methods.1. Using the removeAttr MethodYou can use jQueryâs removeAttr method to remove t
2 min read
How to Disable Text Selection using jQuery?
In this article, we are going to learn how to disable text Selection using jQuery. Disabling text selection refers to preventing users from highlighting or selecting text content on a webpage. Text seÂlection is a common feature in numerous web applications. However, there might arise situations whe
4 min read
How to disable right click on web page using JavaScript ?
Disabling right-click on a web page can be done using the DOM Events. It is a client-side action, and it can be achieved using JavaScript. However, it's important to note that attempting to prevent right-clicking is not a foolproof method for protecting your content, as users can easily bypass it by
2 min read
How to create a Disabled Input using jQuery Mobile ?
jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be creating a Disabled Input using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ hre
1 min read
How to disable a jQuery UI menu?
In this article we will disable a jQuery UI menu Syntax: $(".selector").menu( "disable" );Learn more about selectors and menu options in jQuery. Approach: First, add jQuery UI scripts needed for your project. <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel =
1 min read
How to Disable Input in JavaScript ?
Disabling input fields in JavaScript is a common task when you want to restrict users from interacting with specific form elements. This can be useful in various scenarios, such as preventing modifications to fields that are conditionally locked or ensuring certain inputs are controlled programmatic
2 min read