0

I need to add the attributes from the images inside this div to an JS array and run Supersized, with these images:

       <div id="dgaslides">
            <img src="img/temp/topimg01.jpg" title="Image 1">
            <img src="img/temp/topimg02.jpg" title="Image 2">
            <img src="img/temp/topimg03.jpg" title="Image 3">
        </div>

My JS:

jQuery(function($){

var img_length = $('#dgaslides img').length-1;

var dgaslides = [];

$('#dgaslides img').each( function(i){

    var src     = $(this).attr('src');
    var title   = $(this).attr('title');

    dgaslides.push("{image : '" + src + "', title : '" + title + "'}");

    if(img_length == i){

        $.supersized({

            // Functionality
            slide_interval          :   3000,       // Length between transitions
            transition              :   1,          // 0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
            transition_speed        :   700,        // Speed of transition
            horizontal_center       :   1,          // Centers image horizontally. When turned off, the images resize/display from the left of the page.


            // Components                           
            slide_links             :   'blank',    // Individual links for each slide (Options: false, 'num', 'name', 'blank')
            slides                  :   dgaslides

        });

    }
}); });

It does work, as im getting 3 images in my output, but the link (src) is "Undefined", and the title isn't there either?

The right way to hardcode it is:

var dgaslides = [           // Slideshow Images
                {image : 'img/temp/topimg01.jpg', title : 'Reception'},
                {image : 'img/temp/topimg02.jpg', title : 'Reception 2 og noget mere tekst her'},  
                {image : 'img/temp/topimg03.jpg', title : 'Reception 3'}
            ];

Can anyone help me figuring out what i'm doing wrong?

3 Answers 3

0

You could use jQuery's map method to simplify the task. Following is the code.

jQuery(function ($) {
    var dgaslides = $('#dgaslides img').map(function () {
      return {
        image: $(this).attr('src'),
        title: $(this).attr('title')
      };
    });
    $.supersized({
      slide_interval: 3000,
      transition: 1,
      transition_speed: 700,
      horizontal_center: 1,                   
      slide_links: 'blank',
      slides: dgaslides
    });
});

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

This was clean and simple. Thanks Chandu Dexter
0

I think you need to pass an array of objects and not an array of strings.

dgaslides.push({image : src, title : title });

1 Comment

You're right Musa! Thanks! Actually i just found another question regarding this: stackoverflow.com/questions/16286425/…
0

You're saving the strings to the array rather than the objects.

Here's a JSFiddle with a modified version of your code (without the supersized function) that works as intended: http://jsfiddle.net/SBuXF/

Main change: dgaslides.push({image : src, title : title});

I've output the generated array to the console, followed by your hardcoded one - they're identical.

Edit: Your hardcoded titles didn't match the HTML, so the content of the two arrays is slightly different, but they are both formatted correctly. A corrected fiddle is here: http://jsfiddle.net/SBuXF/1/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.