2

I have an interesting problem that I don't understand. It's a simple jQuery if else statement, but it's not working. Why is it not working?

Code:

$("#div").click(function(){

    if ( $("#div2").css("background-image") === "url(img/image.png)") {
        alert('OK');
    }
    else {
        alert('NG');
    }

});

But when I change .css("background-image") === "url(img/image.png)", for example, to .css('position') === 'absolute', it's working.

5
  • What happens if you alert (or write/log/inspect) $("#div2").css("background-image"). Commented Mar 15, 2012 at 19:45
  • Can you post a snippet of the html you're checking and maybe the corresponding css? Commented Mar 15, 2012 at 19:46
  • 1
    thats weird, why would .css('height') === 'absolute' ever work? Commented Mar 15, 2012 at 19:47
  • oh, sorry its .css('position') === 'absolute' not .css('height') === 'absolute'. Commented Mar 15, 2012 at 19:57
  • @j08691 I tried == insted of === but nothing. Commented Mar 15, 2012 at 20:03

4 Answers 4

4

There could be a subtle difference in the value; alert the value in the else statement to see what's going on. For example,

if ( $("#div2").css("background-image") === "url(img/image.png)") {
    alert('OK');
}
else {
    alert($("#div2").css("background-image"));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yup, the background-image url gets a fully qualified path. Fiddle: jsfiddle.net/TKUET
2

My guess would be that when the css loads, it is taking url(img/image.png) and turing that into http://www.somehost.com/img/image.png. Thus, .css("background-image") would no longer equal "url(img/image.png)".

Instead of checking

.css('background-image') == 'url(img/Image.png)'

check

.css('background-image').indexOf("img/Image.png") != -1

Comments

0

Just shooting in the dark here, but "url(img/image.png)" should probably have single quotes around the argument to url() since that's the way the syntax goes for the CSS.

"url('img/image.png')"

3 Comments

Quoting the path is optional and both single and double quotes are fine.
Sure, both are fine when declaring the path in the stylesheet. The question here, however, is: What does $("#div2").css("background-image") return? A string with or without the single quotes. This might be a browser dependent answer, so doing an indexOf, as Daniel suggests above, might be the safest way to go.
In Firefox at least, the full path is returned and enclosed in double quotes: url("http://thesite.tld/path/to/image.png") (see my fiddle above)
0

It appears that the right answer is to write the full path to the image. So, use url(http://www.myhost.com/img/image.png) instead of url(img/image.png) .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.