Enable Disable Anchor Tags (Links)

Introduction

 
In the previous chapter, we learned about some basic concepts of JavaScript used in Web Applications, like comments, objects, and so on.
 
In this chapter, we learned about how to enable or disable HTML Anchor Links (HyperLink), using JavaScript and jQuery.
 
HTML disabled attribute does not work for HTML anchor tags (HyperLink) and they are still clickable.
 

Enable Disable Anchor Tags (Links) using JavaScript

 
HTML consists of three HTML Anchor Links (HyperLink) and a button. When the button is clicked,  EnableDisableLinks JavaScript function is executed.
 
Inside the function, the value of the clicked button is checked and if the button’s value is disabled, HTML Anchor Links (HyperLink) are disabled i.e. non-clickable and if the button’s value is enabled, HTML Anchor Links (HyperLink) are enabled i.e. clickable.
 

Disabling HTML Anchor Links (HyperLink)

 
In order to disable an HTML Anchor Link (HyperLink), the value of its HREF attribute is copied to the REL attribute and the value of HREF attribute is set to an empty JavaScript function.
 
This makes HTML Anchor Link (HyperLink) disabled i.e. non-clickable.
 

Enabling HTML Anchor Links (HyperLink)

 
In order to enable an HTML Anchor Link (HyperLink), the value of its REL attribute is copied back to the HREF attribute and the REL attribute is removed.
 
This makes HTML Anchor Link (HyperLink) once again enabled i.e. clickable.
  1. <a href="http://facebook.com/">Visit Facebook</a><br /> <a href="http://google.co.in/">Visit Google</a><br />    
  2. <hr /> <input type="button" id="btnEnableDisable" value="Disable" onclick="EnableDisableLinks(this)" />    
  3. <script type="text/javascript">    
  4.     function EnableDisableLinks(btn) {    
  5.         var links = document.getElementsByTagName("a");    
  6.         if (btn.value == "Disable") {    
  7.             btn.value = "Enable";    
  8.             for (var i = 0; i < links.length; i++) {    
  9.                 var href = links[i].href;    
  10.                 links[i].setAttribute("rel", href);    
  11.                 links[i].href = "javascript:;"    
  12.             }    
  13.         } else {    
  14.             btn.value = "Disable";    
  15.             for (var i = 0; i < links.length; i++) {    
  16.                 var href = links[i].getAttribute("rel");    
  17.                 links[i].removeAttribute("rel");    
  18.                 links[i].href = href    
  19.             }    
  20.         }    
  21.     }    
  22. </script>   

Enable Disable Anchor Tags (Links) using jQuery

 
HTML consists of three HTML Anchor Links (HyperLink) and a button. When the button is clicked, click event handler is executed in jQuery.
 
Inside this click event handler, the value of the clicked button is checked and if the button’s value is disabled, HTML Anchor Links (HyperLink) are disabled i.e. non-clickable and if the button’s value is enabled, HTML Anchor Links (HyperLink) are enabled i.e. clickable.
 

Disabling HTML Anchor Links (HyperLink)

 
In order to disable HTML Anchor Link (HyperLink), the value of its HREF attribute is copied to the REL attribute and the value of HREF attribute is set to an empty JavaScript function.
 
This makes HTML Anchor Link (HyperLink) disabled i.e. non-clickable. 
 

Enabling HTML Anchor Links (HyperLink)

 
In order to enable HTML Anchor Link (HyperLink), the value of its REL attribute is copied back to the HREF attribute and REL attribute is removed.
 
This makes HTML Anchor Link (HyperLink) once again enabled i.e. clickable.
  1. <a href="http://facebook.com/">Visit Facebook</a><br /> <a href="http://google.co.in/">Visit Google</a><br />    
  2. <hr /> <input type="button" id="btnEnableDisable" value="Disable" />    
  3. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>    
  4. <script type="text/javascript">    
  5.     $(function() {    
  6.         $("#btnEnableDisable").click(function() {    
  7.             if ($(this).val() == "Disable") {    
  8.                 $(this).val("Enable");    
  9.                 $("a").each(function() {    
  10.                     $(this).attr("rel", $(this).attr("href"));    
  11.                     $(this).attr("href""javascript:;");    
  12.                 });    
  13.             } else {    
  14.                 $(this).val("Disable");    
  15.                 $("a").each(function() {    
  16.                     $(this).attr("href", $(this).attr("rel"));    
  17.                     $(this).removeAttr("rel");    
  18.                 });    
  19.             }    
  20.         });    
  21.     });    
  22. </script>   
The output will look like, as shown below-
 
output
 

Summary

 
In this chapter, we learned how to enable or disable HTML Anchor Links (HyperLink), using JavaScript and jQuery.
Author
Jisny Av
1632 423 287.9k
Next » Variables In JavaScript