0

I'm having a small issue creating objects out of list items in an unordered list. I'm creating a gallery and I need for each gallery thumbnail to be it's own object, so I'm iterating through each list item using jQuery's $.each()

The problem is I don't know how to give each object/li it's own instance name.

Here's the code:

    function galleryButton(){
        this.link
        this.name
        this.image
        this.identifier,
        this.goPage = function(){
        $('.container').animate({opacity : '0'}, 500).load(this.link + ' .galContainer', function(){$('.container').animate({opacity : '1'})});
        return false;
        }
    }

    $(document).ready(function(){
        $.ajaxSetup({
            cache:false
        });

        $('.gallery li a').each(function(node, value){
            this = new galleryButton();
            this.link = $(this).attr('href');
            this.name = $(this).attr('name');
            this.image = $(this + " img").attr('src');
            this.identifier = $(this).attr('data-pic-id');

            $(this).click(this.goPage);
        })

        $('.goback').click(function(){

            var back = $(this).attr('href');
            $('.container').animate({opacity : '0'}, 500).load(back + ' .gallery', function(){$('.container').animate({opacity : '1'})});
                return false;
        });

    });
1
  • You cannot assign to the this keyword!!! Commented Jul 31, 2013 at 19:32

2 Answers 2

1

Don't store your galleryButton into the "this" variable. Make a new var,

var myGalleryButton = new galleryButton();

And update your assignments:

myGalleryButton.link = $(this).attr('href');
/// etc

And then at the end of the .each() function, push myGalleryButton to an array/object for later access.

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

Comments

0

This doesnt make any sense:

   $('.gallery li a').each(function(node, value){
        this = new galleryButton();
        this.link = $(this).attr('href');
        this.name = $(this).attr('name');
        this.image = $(this + " img").attr('src');
        this.identifier = $(this).attr('data-pic-id');

        $(this).click(this.goPage);
    });

You dont want to override this, you want to create a new object like:

        var slide = new galleryButton();
        slide.link = $(this).attr('href');
        slide.name = $(this).attr('name');
        slide.image = $(this + " img").attr('src');
        slide.identifier = $(this).attr('data-pic-id');

So in this case slide is the instance name, but it is only going to exist within the scope of that function each callback function.

Now if you need to able to access these objects then you either need to create the variables outside the function or put them somewhere else accessible. Were it me i would store them in the data for the li like:

        var slide = new galleryButton();
        slide.link = $(this).attr('href');
        slide.name = $(this).attr('name');
        slide.image = $(this + " img").attr('src');
        slide.identifier = $(this).attr('data-pic-id');
        $(this).closest('li).data('slide', slide);

Then you can access them like $(someSelectorToGetTheLI).data('slide').

2 Comments

How do I access the properties of the objects with $(someSelectToGetLI).data('slide)? Would I go $(thing).data('slide).link?
Yes, but you should always make sure you actually have the object - if you havent set it with .data yet then .data will return undefined so using .link directly would throw an error. So something like: sliceObj = $(someSelectToGetLI).data('slide'); if(slide) { // do stuff like slide.link }

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.