?
Solved

Javascript/jQuery if statement not working

Posted on 2011-09-16
5
Medium Priority
?
302 Views
Last Modified: 2012-05-12
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>
0
Comment
Question by:VAN_AL
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 36552838
X is not defined anywhere...

Try:
if(link){
document.write(link)
}else
...
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 36553758
make it

if ( x != undefined && link != undefined && x == link)
{
       document.write(link)
}
else
{
       document.write(altLink)
}
0
 
LVL 1

Author Comment

by:VAN_AL
ID: 36561574
No, sorry non of it worked for me
0
 
LVL 6

Accepted Solution

by:
jjperezaguinaga earned 1400 total points
ID: 36561663
Greetings VAN_AL,

I think your approach to solve the problem is not the right one. You are using $.backstretch(...) without checking the image, and the writing on the document the instance of the plugin.

A better approach would be the following one:
	<script src="<?php bloginfo('template_url'); ?>/js/jquery.backstretch.js"></script>
    <script>
	  <?php $image_link = get_post_meta($post->ID, 'upload_image', true); 
	  if (isset($image_link)) { ?>
		$.backstretch("<?=$image_link?>");
	  <?php } else { ?>
		$.backstretch("<?php bloginfo('template_url'); ?>/images/not-set-default.jpg");
	  <?php } ?>
    </script>

Open in new window


Hope it helps.
Best regards,
-JJ
0
 
LVL 1

Author Comment

by:VAN_AL
ID: 36563256
Thanks @jjperezaguinaga, that worked.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question