7 Replies - 220 Views - Last Post: 13 May 2017 - 01:36 PM

#1 thibaut  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-May 17

jquery checking css value not working

Posted 13 May 2017 - 12:33 PM

I'm frustrated why this simple piece of code doesn't work... i just don't know why it doesn't.. hehe

https://jsfiddle.net/racfyhto/6/

i just don't know why it doesn't do the statement..
Is This A Good Question/Topic? 0
  • +

Replies To: jquery checking css value not working

#2 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6341
  • View blog
  • Posts: 25,584
  • Joined: 12-December 12

Re: jquery checking css value not working

Posted 13 May 2017 - 12:45 PM

Post relevant code directly here in the forum, between code tags, not just as an external link.
Was This Post Helpful? 0
  • +
  • -

#3 thibaut  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-May 17

Re: jquery checking css value not working

Posted 13 May 2017 - 12:47 PM

html:

<div>
    <p id="theme_text"><a href="#" class="theme">verander thema</a></p>
</div>


css:

.theme, .theme:link, .theme:visited, .theme:hover{
      padding: 5px;
      text-decoration: none;
      color: black;
      border: 1px solid black;
    }
    .theme:hover{
      text-decoration: underline;
    }
    #theme_text{
      color: black:
      padding: 0;
      margin: 0;
    }


jquery:

$(document).ready(function(){

    $("a.theme").click(function(e){
    e.preventDefault();

    var $hey = $(".theme").css("color");
		alert($hey);
      if( $hey === "black" || $hey === "rgb(0,0,0)" || $(".theme").css("color") === 'black' ){
        alert("1");
        /*$(".page").css({"background-color":"#edeff2", "color": "black"});
        $("#theme_text").css({"color": "black"});
        $(".theme").css({"border-color": "black"});*/
      }
      else{
        aler("2");
        /*
        $(".page").css({"background-color":"#212735", "color": "white"});
        $("#theme_text").css({"color": "white"});
        $(".theme").css({"border-color": "white"});
        $("footer").css("background-color","black");
        $("hr").css("background-color","white"); */
      }

    });

  });

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6341
  • View blog
  • Posts: 25,584
  • Joined: 12-December 12

Re: jquery checking css value not working

Posted 13 May 2017 - 12:54 PM

You have misspelled alert.

In Google Chrome the value displayed is "rgb(0, 0, 0)", which is not the same as "rgb(0,0,0)".
Was This Post Helpful? 0
  • +
  • -

#5 thibaut  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-May 17

Re: jquery checking css value not working

Posted 13 May 2017 - 12:58 PM

Yes alright, now it goes to the else statement, but i don't know why it never goes to the if part, even though it should be.

This post has been edited by andrewsw: 13 May 2017 - 01:08 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6341
  • View blog
  • Posts: 25,584
  • Joined: 12-December 12

Re: jquery checking css value not working

Posted 13 May 2017 - 01:09 PM

It does if you correct the comparison.
Was This Post Helpful? 0
  • +
  • -

#7 thibaut  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-May 17

Re: jquery checking css value not working

Posted 13 May 2017 - 01:14 PM

View Postandrewsw, on 13 May 2017 - 01:09 PM, said:

It does if you correct the comparison.


Is there a reason why it's not equal to black?
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6341
  • View blog
  • Posts: 25,584
  • Joined: 12-December 12

Re: jquery checking css value not working

Posted 13 May 2017 - 01:36 PM

jQuery docs:

Quote

Note that the computed style of an element may not be the same as the value specified for that element in a style sheet. For example, computed styles of dimensions are almost always pixels, but they can be specified as em, ex, px or % in a style sheet. Different browsers may return CSS color values that are logically but not textually equal, e.g., #FFF, #ffffff, and rgb(255,255,255).

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1