0

I am creating a series of horizontally sliding menus. A brief overview: you have items A,B,C,D,E, and the menu only shows BCD. Press <- to change view to ABC, or press -> to change view to CDE, etc. The item count is variable, so some will be ABCD, some might be ABCDEF.

I figured creating objects would be better than hard-coding these individually.

Here is my code:

function Slider(uniqueName, subtitles, icons, descripts, links){
    var thisSlider = '#'+uniqueName; //in this example uniqueName = 'mbp'
    $(thisSlider).html('<div class="sliderContainer" id="'+uniqueName+'">\
            <div class="sliderArrowContainer">\
                <a href="javascript:void(0);" id="matSliderLeft"><img class="sliderLeft" id="'+uniqueName+'Left" src="../images/misc/lArrowGrey.png"></a>\
            </div>\
            <div class="sliderBlock">\
                <div class="sliderIconHolder"><img class="sliderImg" src="../images/misc/'+icons[0]+'"></div>\
                <div class="sliderSubtitle">'+subtitles[0]+'</div>\
                <div class="sliderText">'+descripts[0]+'</div>\
            </div>\
            <div class="sliderBlock">\
                <div class="sliderIconHolder"><img class="sliderImg" src="../images/misc/'+icons[1]+'"></div>\
                <div class="sliderSubtitle">'+subtitles[1]+'</div>\
                <div class="sliderText">'+descripts[1]+'</div>\
            </div>\
            <div class="sliderBlock">\
                <div class="sliderIconHolder"><img class="sliderImg" src="../images/misc/'+icons[2]+'"></div>\
                <div class="sliderSubtitle">'+subtitles[2]+'</div>\
                <div class="sliderText">'+descripts[2]+'</div>\
            </div>\
            <div class="sliderArrowContainer">\
                <a href="javascript:void(0);" id="matSliderRight"><img id="'+uniqueName+'Right" src="../images/misc/rArrowGreen.png"></a>\
            </div>\
        </div>');
    $(uniqueName).data( { subtitles: [subtitles], icons: [icons], descripts: [descripts], links:[links] } );

}


$(".sliderLeft").live("click", function() {
    var cur = this.id;
    cur = cur.substr(0,3); //this is bc all var uniqueNames are 3char, in this case it is 'mbp'
    var tempData = $('#'+cur).data(subtitles[0]);
    alert(tempData); //this alerts undefined
}); 

My alert at the end alerts undefined. Obviously something in my syntax is very wrong, but I'm also wondering if my approach in general is wrong? The vars passed into Slider(); are arrays (aside from uniqueName), so I'm trying to store the arrays using jQuery .data(); into the main container.

Any help on how to store multiple arrays being passed to Slider(); into .sliderContainer, and then accessing it by obtaining the .sliderContainer's ID by clicking on different parts... would be very helpful.

(Should I be using JSON?)

Thank you!

1 Answer 1

1

I'm pretty sure what you want to do is:

var tempData = $('#'+cur).data('subtitles')[0];

You're accessing the data property subtitles which you know to be an array, and you want the first element. The above code does none of the checking and validation that it should.

Also, this bit:

$(uniqueName).data( ...

You should have used thisSlider as the unique name does not include the hash. Change to

$(thisSlider).data( ...
Sign up to request clarification or add additional context in comments.

4 Comments

This still doesn't work -- Uncaught TypeError: Cannot read property '0' of undefined. Thank you, though.
Looks like there may be a problem where I'm assigning data, will look into it -- your syntax has helped me in alerting test data in the console :)
@HC_ - if you create a jsfiddle.net of what you're trying to do then its easier to guide you in the right direction on a better approach
So your syntax correction was half the mistake, the other half was missing a '#' in the data assignment @__________@ thank you for your help!

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.