Link to home
Start Free TrialLog in
Avatar of VAN_AL
VAN_AL

asked on

Javascript/jQuery if statement not working

I'm currently using the backstretch.js script for full screen background images, I've got it working but I would like to add an if statement to check if an image is set, which are set via custom post types display that image, if not, the default image should be link to image

This is what I have so far, but it doesn't work and need help fixing it:

<script src="<?php bloginfo('template_url'); ?>/js/jquery.backstretch.js"></script>
    <script>
      var link = $.backstretch("<?php echo get_post_meta($post->ID, 'upload_image', true); ?>")
      var altLink = '<?php bloginfo('template_url'); ?>/images/not-set-default.jpg'
        if (x == link) {
             document.write(link)
        } else {
             document.write(altLink)
        }

    </script>
Avatar of Kyle Hamilton
Kyle Hamilton
Flag of United States of America image

X is not defined anywhere...

Try:
if(link){
document.write(link)
}else
...
Avatar of Gurvinder Pal Singh
make it

if ( x != undefined && link != undefined && x == link)
{
       document.write(link)
}
else
{
       document.write(altLink)
}
Avatar of VAN_AL
VAN_AL

ASKER

No, sorry non of it worked for me
ASKER CERTIFIED SOLUTION
Avatar of jjperezaguinaga
jjperezaguinaga
Flag of Mexico image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of VAN_AL

ASKER

Thanks @jjperezaguinaga, that worked.