I wrote those simple lines in order to show an .elemproduct div only if it has a background-image set.
I developed under Chrome and just realised that this code is only working under Webkit browsers. When failing, it shows up every .elemproduct as if the if statement was not recognized.
function hideBox() {
var $bg = $('.elemproduct');
$bg.each(function() {
if ($(this).css("background-image") === "url(####)") {
return true;
}
else {
$(this).show();
}
});
return true;
};
Any ideas ?
I’d suggest that it might be because the
background-imageis never going to be exactly equal (===) to the path of the (presumably) image directoryhttp://www.infiniscale.com/beta/.If you want to simply hide those
.elemproductelements without abackground-imageit seems a little easier to use:JS Fiddle demo.
Edited to compensate with the problem that an empty
url()in thebackground(orbackground-image) property is filled with the URL of the current page. This makes for quite a bulkyifstatement, but it does at least work reliably (so far as I can currently tell), which is an improvement on the above. Latest iteration:JS Fiddle demo.
Edited and amended the above, to remove the unnecessary variable (
srcString) and its multiple calls to.replcace():JS Fiddle demo.
Edited in response to comment from OP:
The
iftests for three conditions:!imgString, if there’s no string returned from thebackground-imageproperty of the.css()method.imgString == 'none', because jQuery (sometimes, or possibly always) returns ‘none’ if there is no definedbackground-image.imgString.indexOf(href) > -1. This looks at theimgStringvariable to see if it contains the string contained by the variablehref. If the string is found it returns the position at which it was found; if it was not found it returns-1.JS Fiddle demo.
JS Fiddle demo.
JS Fiddle demo.
JS Fiddle demo.
This is to check that the empty
url()in CSS isn’t simply being filled automatically by the browser.Edited after being pointed at the URL for the page and finding that the use of PHP and GET made things a tad more awkward than I’d imagined.
Using the following in the console (certainly in Chromium) worked:
The above, working in JS Fiddle and implemented into the
hideBox()function:A demonstration of the above is available at JS Fiddle!