0

I'm trying to check if a div has a certain background image, and if it has the image, then the page should navigate to a url.

However, I can't get it to work, what am I doing wrong?

case key.enter:
    if (letterIndex == 0 && $(this).hasClass('active')) {
           $(this).prev().remove();
    } else if ($('.content').css("background-image") === "url(img/screen-check.jpg)") { 
            // go to url
    }  else {
            $(this).closest('li').toggleClass('active');
    }
    break;
Khalid
  • 4,730
  • 5
  • 27
  • 50
pistevw
  • 432
  • 4
  • 15
  • Can you give jsfiddle sample code? – DonX May 18 '15 at 10:12
  • Is your question the same as this: http://stackoverflow.com/questions/1226714/how-to-get-browser-to-navigate-to-url-in-javascript – James Newton May 18 '15 at 10:12
  • 1
    working fine [here](http://jsfiddle.net/870oz2gv/1/) – Amit Soni May 18 '15 at 10:15
  • @JamesNewton Not really, I know how to navigate to url, but I want to do the navigation only if the div has a certain image. – pistevw May 18 '15 at 10:16
  • your comparison works in principle: [https://jsfiddle.net/govi20/u37cxxbh/](https://jsfiddle.net/govi20/u37cxxbh/) There must be something wrong with the parenting checks – Ben Philipp May 18 '15 at 10:23

1 Answers1

0

Not sure if below code works. Please try and let me know

 case key.enter:
        if (letterIndex == 0 && $(this).hasClass('active')) {
               $(this).prev().remove();
        } else if ($('.content').css("background-image") &&$('.content').css("background-image").indexOf('img/screen-check.jpg') >-1 ) { 
                // go to url
        }  else {
                $(this).closest('li').toggleClass('active');
        }
        break;
DonX
  • 16,093
  • 21
  • 75
  • 120
  • @jdo Can you please tell me what is the value of $('.content').css("background-image") ? Are you sure that if loop is getting executed? – DonX May 18 '15 at 10:32
  • It doesn't seem like the if statement is executed at all. – pistevw May 18 '15 at 10:57