Solved

Disable hyperlink after clicking it

Posted on 2006-06-21
8
4,971 Views
Last Modified: 2012-06-27
I have a hyper link defined as follows:

<a href='...' id='submitButton'><img src="img/submit.gif" border="0"></a>

When the link is clicked, I would like to disable this link. Can it be done?

Thanks!
0
Comment
Question by:yongsing
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 16

Expert Comment

by:Swapnil Piparia
ID: 16950504
Hi yongsing,

it is not possible to disable hyperlink, but you can hide hyperlink after click on it once.

by document.all.hyperlinkname.style.display = 'none'
and for showing it back document.all.hyperlinkname.style.display = ''


Regards,
NetSwap
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16950679
It is possible.
Like this:



<a href='http://www.experts-exchange.com/Q_21893830.html' id='submitButton' onClick="this.onclick=function(){return false}" target="EE"><img src="img/submit.gif" border="0"></a>

The target is only for demo. It works also for target="_self".


0
 
LVL 7

Expert Comment

by:geordie007
ID: 16950700

Netswap is right, but beware. the document.all method is an old internet explorer reference. nowadays, all modern browsers - including internet explorer - use document.getElementById. using your example, you could do this:

document.getElementById('submitButton').style.display = 'none';

this would remove it completely though, so you would get document flow issues, as any content below it would move up, any content to the right, would move across, etc. to combat that you could say:

document.getElementById('submitButton').style.visibility= 'hidden';

of course, you may want to keep the image there, but simply make it so that it didn't work anymore. to do this, you could change the href of the link, like this:

<script type="text/javascript">
var clicked = false;
function isClicked(){
      if(clicked){
      document.getElementById('submitButton').href = "javascript:;";
      }
      clicked = true;
}
</script>

<a href="(MY ORIGINAL LINK OR FUNCTION)" onclick="isClicked()" id="submitButton"><img src="img/submit.gif" border="0"></a>

the first time it's clicked, clicked is false, so it doesn't change the href, and your original action happens. the second time its clicked, clicked is now true, so it sets your href to "javascript:;", which is the equivalent of "do nothing at all".

hope this helps.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:callrs
ID: 16950783
Just created & tested this. It works! : )
change http://www.google.com to the link you want your user to go to.

<a href=""' id='submitButton'  onclick="this.outerHTML='<img src=img/submit.gif border=0>';window.open('http://www.google.com');"><img src="img/submit.gif" border="0"></a>
0
 
LVL 30

Accepted Solution

by:
callrs earned 30 total points
ID: 16950792
Even better:
<a href=""' id='submitButton'  onclick="this.outerHTML=this.innerHTML;window.open('http://www.google.com');"><img src="img/submit.gif" border="0"></a>
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16950977
document.all and this.outerHTML is IE only.
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 16952923
I like Zvonko's since it is cross browser and quite elegant :D *bows down* heh (ok, this comment is probably just wasted space, but I did really like it; I would've never thought to set its onclick to return false, lol)

Ted
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16955060
Thanks Ted :)
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question