4

I have 2 hyperlinks. when i click on one hyperlink the another hyperlink should be disabled means it should be seen but not clicked by any user

Please help me

 <a href="">hiii </a> 
 <a id="check" href="google.com">bye</a>

in JavaScript

$('#check').attr('disabled', true);

but it is not working

Anoop
  • 23,044
  • 10
  • 62
  • 76
Delta
  • 229
  • 1
  • 5
  • 12

6 Answers6

4

Using java script you can disable the hyper link by adding a .disabled class as seen below:

  .inactive //add this class to the link if you want to disable it 
{
    pointer-events: none;// this will disable the link
    cursor:default;
}

then use .inactive class in appropriate line...

sanj
  • 85
  • 10
3

Try below

$('.my-link').click(function () {return false;});

To re-enable it again, unbind the handler:

$('.my-link').unbind('click');

or

$('.my-link').attr('disabled', 'disabled');

Use this to re-enable it:

$('.my-link').attr('disabled', '');

Thanks,

Siva

SivaRajini
  • 7,225
  • 21
  • 81
  • 128
3

Below is the code you need.

<a id="gLink" href="http://google.com">click me</a><br />
<a onclick="disableLink()" href="#">Disable link</a><br />
<a onclick="enableLink()" href="#">Enable link</a>

javsacript functions:

 function disableLink() {
        var a = document.getElementById('gLink');
        a.href = "#";
    }

    function enableLink() {
        var a = document.getElementById('gLink');
        a.href = "http://google.com";
    }
Dropout
  • 13,653
  • 10
  • 56
  • 109
Sudarshan Tanwar
  • 3,537
  • 3
  • 24
  • 39
1

for e.g if you have

  <a id="link1" href="page1.php">One</a> <a id="link2" href="page2.php">Two</a>

document.getElementById('link1').onclick = function()
 {
   document.getElementById('link1').disabled = true;
   document.getElementById('link2').disabled = false;
 };

  document.getElementById('link2').onclick = function()
 {
   document.getElementById('link1').disabled = false;
   document.getElementById('link2').disabled = true;
 };
sanj
  • 85
  • 10
  • you have to create another function as well forlink2 document.getElementById('link2').onclick = function() { document.getElementById('link1').disabled = false; document.getElementById('link2').disabled = true; }; – sanj May 24 '13 at 11:39
0

You can use jQuery onClick event (or .on("click") / .live("onClick") or whatever you prefer) to change to attribute of the link like this:

$('.my-link').attr('disabled', true);

Short example:

<a href='#' id='link1'>First</a>
<a href='#' id='link2'>Second</a>
<script>
    $("#link2").onClick(function(){
        $('#link1').attr('disabled', true);
        };
    }
</script>
Dropout
  • 13,653
  • 10
  • 56
  • 109
0

That's all I know

<html>
 <head>
 <script language="javascript" type="text/javascript">
  window.onload = firstLoad;
  function firstLoad() {
  document.getElementById("lesson").href = "";
   document.getElementById("posttest").href = "";
   }
     function clickHome() {
    document.getElementById("pretest").href = "";
     document.getElementById("lesson").href = "lesson.html";
    }
     function lessonRead() {
      document.getElementById("posttest").href = "posttest.html";
     document.getElementById("lesson").href = "";
       }
     </script>
         </head>
    <body>
      <a href="home.html" id="home" onClick="clickHome()">Home</a> | 
     <a href="pre-test.html" id="pretest">Pre-test</a> | 
     <a href="lesson.html" id="lesson">Lesson</a> | 
     <a href="post-test.html" id="posttest" onClick="lessonRead()">Post-test</a> | 
     <a href="about.html" id="about">About</a> | 
     </body>
     </html>