Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to click on a link and get its color?

OK, so this sounds simple, using jquery's 'click' function.

The thing is I want the ORIGINAL color of the link NOT its hovered color - e.g. if the link is green with a hover state of orange, I wat to grab the GREEN color.

I've tried as best as I can and shown it in a fiddle

Anyone got any ideas?

EDIT: apologies, but as a number of eagle eyes have spotted (thanks to them for pointing it out) green is actually "rgb(0, 128, 0)", NOT rgb(0, 255, 0) as shown in my original fiddle.

like image 369
Steve Avatar asked Aug 31 '12 14:08

Steve


People also ask

How do I change the color of a clicked link?

To change the color of hyperlink text, click Hyperlink, and then click More Colors. To change the color of the followed hyperlink text, click Followed Hyperlink, and then click More Colors.

How do I make text a link color?

To change the color of links in HTML, use the CSS property color. Use it with the style attribute. The style attribute specifies an inline style for an element. Use the style attribute with the CSS property color to change the link color.

How do I make an active link a color in HTML?

HTML body tag example TEXT = The color of text. LINK = The color of links. VLINK = Visited link color. ALINK = Color of the active link or the color the link changes to when clicked.

How do I make a link blue again?

So, just right-click on the hyperlink and from the context menu select "Edit Hyperlink". It brings up the edit dialog box. Click on "OK". The hyperlink is returned to it's original blue state.


4 Answers

Store it in jQuery's data object:

$('a').each(function() {
    $(this).data('color', $(this).css('color') );
})
.click(function() {
    alert( $(this).data('color') );
});

Here's your fiddle: http://jsfiddle.net/sVDYe/4/


For better performance, I'd use the static methods in the loop. They're much faster:

$('a').each(function() {
    $.data(this, 'color', $.css(this, 'color') );
});

Here's the fiddle: http://jsfiddle.net/sVDYe/13/

like image 141
Joseph Silber Avatar answered Sep 30 '22 19:09

Joseph Silber


Try like this

http://jsfiddle.net/dadviegas/hfHBh/

like image 44
DadViegas Avatar answered Sep 30 '22 20:09

DadViegas


Try below approach..

DEMO: http://jsfiddle.net/sVDYe/33/

$("a").click(function(e) {
    e.preventDefault();
    var tmpLink =$(this).clone();
    tmpLink.appendTo($(this).parent());    
    var acolor = tmpLink.css("color");
    tmpLink.remove();

    if (acolor == 'rgb(255, 165, 0)') {
        alert('wrong color - its ORANGE =' + acolor);
    } else if (acolor == 'rgb(0, 128, 0)') {
        alert('CORRECT color - its GREEN =' + acolor);
    }
});

As pimvdb pointed out.. Green is RGB(0,128,0)

like image 21
Selvakumar Arumugam Avatar answered Sep 30 '22 18:09

Selvakumar Arumugam


You can handle the color change using Jquery instead of css and store it:

http://jsfiddle.net/sVDYe/20/

var hoverColor;

$("a").hover(function () {
  hoverColor = $(this).css("color");
  $(this).css("color", 'orange');
}, function () {
  $(this).css("color", 'green');
});

$("a").click(function (e) {
  e.preventDefault();
  var acolor = hoverColor;
  if (acolor == 'rgb(255, 165, 0)') {
    alert('wrong color - its ORANGE =' + acolor);
  } else if (acolor == 'rgb(0, 128, 0)') {
    alert('CORRECT color - its GREEN =' + acolor);
  }
});

Also green is

rgb(0, 128, 0)

like image 43
Liam Avatar answered Sep 30 '22 18:09

Liam