2

How do I set the visited color of an "a" tag. here is the code I have

theobj.find('a:visited').css('color', thejson.WidgetInfo.TextColor);

The above code is not working. I can only use inline css. Is there a way to do this?

Luke101
  • 63,072
  • 85
  • 231
  • 359

5 Answers5

5

You could construct a style tag, and then write it to the page.

var visited_link_styling = "<style> a:visited{ color:'red'; } </style>";
$('head').append( visited_link_styling );

This might get a little frustrating because JavaScript isn't good at multiline strings.

jastr
  • 881
  • 1
  • 9
  • 19
2

I would suggest you to have a css class and set that class instead but since you can only use inline style you can try this

theobj.find('a').attr("style", "color:#000000 !important");
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
2

There isn't a visited selector in jQuery that I am aware, but a similar question points to a plugin to handle this Remy Sharp Visited Plugin

Community
  • 1
  • 1
Jeremy Battle
  • 1,638
  • 13
  • 18
0

Is there a reason you are wanting to use jQuery and not pure CSS? Should visited links behave differently than unvisited links?

Depending on your answers to the above questions, the solutions will vary.

CSS:

a:hover { color: #000; }

jQuery (for multiple link color attributes):

var ele = $("#widget a"); // Replace the desired element/object here
var eleColor = ele.css('color'); // Grab what the element's color is
if(eleColor != '#000000' || eleColor != '#000'){ // If it doesn't match X or Y
ele.css("color","000"); // Set to default color
}

^Substituting desired colors above.

Or approach 2:

$("#widget a").css("color","000"); // Set all links to #000

Or approach 3:

$("#widget a").click(function(){ $(this).css("color","000"); });

Working example: http://jsfiddle.net/9N5Xe/

Robert
  • 8,717
  • 2
  • 27
  • 34
  • Yes, the way the program is setup..I can only use inline css. It would take way too long to integrate pure css in the program. – Luke101 Aug 11 '11 at 17:39
  • This is correct..I want all links the same color no matter if its visited or not. This works in MS explorer but not in firefox – Luke101 Aug 11 '11 at 18:19
0

You can't actually set any properties on an element that only apply to some pseudoselector like visited. CSS documents are declarative, in that you include the CSS in your document and then the set of rules becomes available. JavaScript is not declarative, but executed, which means you can only catch some event and then respond to that. In other words; you can select all the links that are visited and set the color for each, but you can't set the color for visited links.

Now, in order to achieve what you want, you can set a 'live' event handler for the click event on each anchor and then apply the CSS accordingly.

Out of curiosity; why don't you just set the rule in a style element or in a css document?

Pelshoff
  • 1,464
  • 10
  • 13
  • Thanks for your comment..For this particular project it is not possible to use plain stylesheets as it will be used in a widget. Therefore when the widget is placed on remote website it will apply to the whole website – Luke101 Aug 11 '11 at 17:44