2

i want to disable my link when it is clicked . checkletter is a function which checks if letter is available in word for my hangman game.

 <span id="A"><a href="javascript:checkLetter('A');">A</a></span> 

i tried using this a.visited{ display:none; }

it doesn't seem to be working

Please advice!!

3
  • 1
    It would be a:visited instead of a.visited but it'll hide the link, not deactivate it Commented Dec 29, 2013 at 19:23
  • To be clear are you trying to disable the function from being called, or change the appearance of the link? Commented Dec 29, 2013 at 19:25
  • sorry it was just a typo i used a:visited{ Commented Dec 29, 2013 at 19:25

3 Answers 3

3

document.querySelector('#A a').style.pointerEvents = 'none';

** http://caniuse.com/#search=pointer-events

Sign up to request clarification or add additional context in comments.

2 Comments

Not gonna downvote since it's true, but this limits cross-browser compatibility. Nice find though.
Agree, this isn't cross browser solution now (thanks, IE!). That's why I've posted link to caniuse.com :) link.href = null [1] solution would be the best one if browser compatibility is an issue. [1]: stackoverflow.com/questions/20829143/…
1

Can you just use your checkLetter function?

function checkLetter(letter) { // Do stuff document.getElementById(letter).style.display = "none"; } 

update

To disable the link instead of hiding it (as requested in comment) see this link: http://jsfiddle.net/J67eY/1/

1 Comment

thanks..this seems to be working but is there a way i can disable my link instead of making it invisible
1

EDIT:

Very Fun Fiddle

Long story short: assuming link is assigned to your link:

link.href = null; 

will disable the link entirely.

I got excited and posted too quickly. Here's what you want :)

var links = document.links || document.anchors || document.getElementsByTagName('a'); for (var i = 0, j = links.length; i < j; i++) { addEvent(links[i], 'click', disable); } function disable(evt) { var e = evt || window.event, link = (e.currentTarget) ? e.currentTarget : e.srcElement; checkLetter(link.innerHTML); link.href = null; return false; } function addEvent(element, myEvent, fnc) { return ((element.attachEvent) ? element.attachEvent('on' + myEvent, fnc) : element.addEventListener(myEvent, fnc, false)); } 

This code also assumes you removed javascript:checkLetter('A') from your link's href="". Instead, I call it inside the function using the letter that it is (link.innerHTML)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.