0

**i am trying to put an event handler based on CSS condition and it doesn't work,any help please ** here is my HTML code :

<!Doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jquery test</title>
<style>
.x{color:red;}
</style>
</head>
<body>
    <input id="trigger" type="button" value="test"/>
<p class="x" >some text</p>
<p>another text</p>
<script type="text/javascript" src="js/jQuery.js"></script>
<script type="text/javascript" src="js/match.js"></script>
</body>
</html>

and here is the jquery code :

$('#trigger').click(function(){
    if($('p.x').css("color") === "red")
    {
       var text= $('p').text();
       alert(text);
    }
});
A.mansour
  • 3
  • 1
  • 7
    The browser doesn't return `red`, which you could have figured out with simply logging the value, it returns RGB(A) colors. – adeneo Jul 02 '14 at 15:38
  • This answer may help to understand how it works: http://stackoverflow.com/a/6177502/1249581. – VisioN Jul 02 '14 at 15:41
  • It would probably be easier to check for a class rather than checking the colour. Is there a reason you want to do it this way? – Razzildinho Jul 02 '14 at 15:51
  • thank u ,i tried the (RGB) it didn't work also, actually i am trying to do an event handler based on some css condition like the transform:translate(10em ,0) and i want to use it in a wheel containing letters rotatable by mouse ,when the desired letter is at some point(x,y coordinates) the letter generated in another place.and thank u all again – A.mansour Jul 02 '14 at 16:20
  • oh i am sorry, it works for the rgb color i hope it works for the rest attributes thanks! – A.mansour Jul 02 '14 at 16:32

1 Answers1

0

Working JSFiddle

As adeneo said, 'red' gets converted to RGB.

Javascript:

$('#trigger').click(function(){
    if($('p.x').css("color") == 'rgb(255, 0, 0)'){
        var text= $('p.x').text();
        alert(text);
    }
});
Razzildinho
  • 2,564
  • 1
  • 19
  • 32
  • if you just take an answer already provided in the comments you could at least provide proper js. like telling him .click is a bad practice due to it's slowness. and to better use on('click'.. for instance. – gulty Jul 02 '14 at 15:51