jdoyle0116
jdoyle0116

Reputation: 83

Setting color of visited link with Javascript

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?

Upvotes: 2

Views: 2791

Answers (2)

Biswajit Chatterjee
Biswajit Chatterjee

Reputation: 41

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.

Upvotes: 0

Zach Leighton
Zach Leighton

Reputation: 1941

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

Upvotes: 4

Related Questions