1

I having a hyperlink generation like below

$element.html(indicator.link(url)); 

but it comes in typical hyperlinks blue color. How can I change the color of the font of this hyperlink in javascript?

3
  • 2
    Use CSS for all styling. You could add a class to your jQuery element, where the class has stylings for the links. See stackoverflow.com/a/11955727/864233 for examples of the CSS on the link. Commented May 31, 2016 at 19:24
  • 1
    what's wrong with a good old css selector? Commented May 31, 2016 at 19:26
  • do you have the id of this element after you create it? Commented May 31, 2016 at 19:29

3 Answers 3

4

It's best to do this with CSS. JavaScript is for modifying behavior, while CSS is used for styling. Using JavaScript for this is highly frowned upon.

That said, this question depends on what type of state the link is in. For unvisited links, use the :link pseudoselector.

a:link { color: green; }
<a href="http://www.espn.com">Visit ESPN!</a>

For links you've already visited, you can now use the :visited pseudo-class.

a:visited { color: red; }
<a href="http://www.youtube.com">Visit YouTube!</a>

For links you are just hovering over, use :hover

a:hover { color: pink; }
<a href="http://www.pineapplesandoranges.com">Pineapples and Oranges</a>

You should absolutely not use JavaScript. But here is the solution for it anyways.

var link = document.getElementById("my-link"); link.style.color = "green";
<a id="my-link" href="http://www.zebras.com">Zebras</a>

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

Comments

1

try:

$element.html(indicator.link(url)); $element.css('color','#000'); 

Or:

$('a').css('color','#000'); 

But this would make all links with a tag turn black.

1 Comment

I tried this but it doesn't change the default color of the hyperlink.
1

well look like if like you are working with Jquery

One option is with the .css()

$element.html(indicator.link(url)); $element.css( "color", "red" ); 

1 Comment

I tried this but it doesn't change the default color of the hyperlink.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.