Setting color of visited link with Javascript

2.5k Views Asked by At
divText += '<div class="single-article"> ';
divText += '<h2><a href="#" onClick="javascript:displayArticleDetail('+json._embedded.articles[i].articleId+', \''+escapedLinkTitle+'\'); setVisited();" \>'+json._embedded.articles[i].title+'</a>&nbsp;&nbsp;';

I need some assistance setting the color of a visited link. Having some trouble with this. I cannot use pure CSS :visited because this goes and changes the link color of every single link, not just the visited (clicked).

How would I go about doing this with Javascript?

2

There are 2 best solutions below

4
On

You need to give more specificity to that anchor tag.

Try giving it an id field and use a#id:visited when styling it

You could also use class and select it with a.class:visited

0
On

Ideally this should solve your problem,

a:visited {
    background-color: yellow;
}

but even if you want to handle it with javascript:

you can use

document.getElementById("#elementID").style.color = "#ff0000";

or if you are using jQuery you can add a class to the element on click.