String comparison of 'content' property read from <div> element by JQuery

56 views Asked by At

I am only asking this, because I am really confused about this and even trimming doesn't help getting this comparison pass.

Code:

HTML

<div id="someDiv"></div>

CSS

#someDiv {
    content: "someValue";
}

JQuery

$(document).ready(function() {
    var value = $("#someDiv").css("content");
    value = $.trim(value);
    console.log(value);


    if (value == "someValue") {
        console.log("good");
    }
    else {
        console.log("bad");   
    }


});

Why does the string comparison fail here?

JSFIDDLE

EDIT: The strange thing that got me confused was that this comparison was OK in Chrome/Chromium but not in Firefox.

2

There are 2 answers

1
kaveh On BEST ANSWER

What about replacing quotes and double quotes in the beginning and end of content? Maybe adding something like this after trimming? value = value.replace(/^(\"|\')|(\"|\')$/g, "");

FIDDLE (Works on both Chrome and Firefox)

8
depperm On

Value has single quotes included, literally, change the if to

if (value == "'someValue'") 

to pass,

or add this after trim and keep the if as is

value=value.replace(/'/g,'');

or change the if to

if (eval(value) == "someValue")