jQuery to load random image from array using chickendinner js returning undefined in Chrome

0 votes

I have the following jQuery script setup using the info pages from the developer's site, but Chrome keeps returning 'undefined'.

<script src="//code.jquery.com/jquery-1.11.2.min.js""></script>

(function($) {
    $('.js-chickendinner-bg').chickenDinner = {
        defaults: {
            path: 'http://127.0.0.1:4000/covers/',
            altTag: ['Cover'],
            fadeInTime: 1800,
            cssBG: 'true',
            TheImages: ['cover1.jpg', 'cover2.jpg', 'cover3.jpg', 'cover4.jpg', 'cover5.jpg', 'cover6.jpg', 'cover7.jpg', 'cover8.jpg', 'cover9.jpg', 'cover10.jpg', 'cover11.jpg', 'cover12.png', 'cover13.png', 'cover14.png']
        }
    };

    $.fn.extend({
        chickenDinner: function(options) {
            $.extend({}, $.chickenDinner.defaults, options);
            return this.each(function() {
                var TheImages = options.TheImages;
                var RandomMath = Math.floor(Math.random() * TheImages.length);
                var SelectedImage = TheImages[RandomMath];
                var imgPath = options.path + SelectedImage;
                var altTag = options.altTag;
                // Fade in Times
                var fadeInTime = options.fadeInTime;
                //Fade In animation - hide first
                $(this).css('display', 'none').fadeIn(fadeInTime);
                if (options.cssBG == 'true') {
                    $(this).css('background-image', 'url(' + imgPath + ')');
                } else {
                    $(this).attr({
                        src: imgPath,
                        alt: altTag
                    });
                }
            });
        }
    });
}(jQuery));
<div class="site-header-container has-cover" class="js-chickendinner-bg" style="background-image: url({{ site.cover | prepend: site.baseurl }});">
  <div class="scrim {% if site.cover %}has-cover{% endif %}">
    <header class="site-header">
      <h1 class="title">{{ site.title }}</h1>
      {% if site.subtitle %}
      <p class="subtitle">{{ site.subtitle }}</p>{% endif %}
    </header>
  </div>
</div>

 Run code snippet

Expand snippet

I want to use this script to randomize image loads on my website's homepage, built with and served by Jekyll (hence the local server for testing above).

It is my understanding that the div class="js-chickendinner-bg" should work with the jQuery function the way I have it, but something is still apparently off.

Jul 27, 2022 in Web Development by gaurav
• 23,260 points
1,001 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

It's because you're choosing a random number from the length of your array, but the array index starts at 0. In your case the random number will be between 1 and 14, but your array index range will be from 0 to 13. Due to that your random number will sometimes be greater than the highest index of your array.

If you modify this line to subtract 1 from the random value:

var RandomMath = Math.floor(Math.random() * TheImages.length) - 1;

That will prevent your random value from being too large.

answered Jul 28, 2022 by rajatha
• 7,680 points

edited Mar 5

Related Questions In Web Development

0 votes
1 answer

Using Jquery Ajax to retrieve data from Mysql

You forgot the table tag Write echo"<table>" before ...READ MORE

answered Dec 30, 2022 in Web Development by anonymous

edited Mar 5 23,609 views
0 votes
1 answer

How to loop through array in jQuery?

Generic loop: var i; for (i = 0; i ...READ MORE

answered Jun 28, 2022 in Web Development by rajatha
• 7,680 points
2,366 views
0 votes
0 answers

How to add a link in Jquery PrettyPhoto to download the image

I am using Jquery PrettyPhoto to have ...READ MORE

Jun 29, 2022 in Web Development by gaurav
• 23,260 points
1,005 views
0 votes
1 answer

How to Fade In/Out multiple texts using CSS/jQuery like on Droplr?

The jQuery fadeIn() method is used to ...READ MORE

answered Jun 29, 2022 in Web Development by rajatha
• 7,680 points
1,623 views
0 votes
0 answers

convert base64 to image in javascript/jquery

I have written some code for image ...READ MORE

Jun 29, 2022 in Web Development by gaurav
• 23,260 points
3,359 views
0 votes
0 answers

How to get mobile number from facebook login using jquery?

I am trying to work facebook login ...READ MORE

Jul 19, 2022 in Web Development by gaurav
• 23,260 points
941 views
0 votes
1 answer

What is jQuery?

Hey, jQuery is a fast and concise JavaScript ...READ MORE

answered Feb 14, 2020 in JQuery by kartik
• 37,520 points
1,568 views
0 votes
1 answer

Uncaught TypeError: Cannot read property 'msie' of undefined - jQuery tools

Hello, Use the following script tag in your ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,840 points
16,601 views
0 votes
1 answer

Uncaught Error: Bootstrap's JavaScript requires jQuery

Hello @kartik, You have provided wrong order for ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,840 points
23,878 views
0 votes
1 answer

How to make Bootstrap popover Appear/Disappear on hover instead of click?

Hello @kartik, Set the trigger option of the popover to hover instead ...READ MORE

answered May 12, 2020 in JQuery by Niroj
• 82,840 points
3,578 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP