0

How can I grab a value from an array and call it within .append()?

In this code, the array I'm trying to pull content from is titles, and append it to the list item with the class .activeSlide. However, I get UNDEFINED on hover instead.

$(document).ready(function() {
    var titles = ["Title 1", "Title 2", "Title 3"];
    $("#slideshow").before("<ul id='slideshow-nav'></ul>")
    .cycle( {
        fx:                         "scrollVert",
        rev:                            "scrollVert",
        speed:                      600,
        timeout:                    0,
        pagerEvent:             "mouseover", 
        pager:                      "#slideshow-nav",
        pagerAnchorBuilder: function (index) {
            return "<li>" + titles[index] + "</li>";
        }       
    });
     $("#slideshow-nav li").hover(function(index) {
        $(this).parent().children(".activeSlide").append("<a href='#'>" + titles[index] + "</a>");
    }, function () {
        $("a").remove();
    });
});
2
  • Simply use titles[i] where i is 0, 1 or 2 (the index of your array) Commented Jun 11, 2013 at 22:04
  • That works if I want to call the same value on every list item, but it needs to change according to the index order depending on which list item has the .activeSlide class. Commented Jun 11, 2013 at 22:12

1 Answer 1

2
$("ul li").hover(function () {
    var ind = $(this).index();
    if ($(this).hasClass('activeSlide')) {
        $(this).append("<a href='#'>" + titles[ind] + "</a>");
    }
}, function () {
    $("ul li a").remove();
});

FIDDLE

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

6 Comments

That called all three values at once.
@ElliPetersen can you be more clear in what you want to do, it seems I didn't quite understand it right.
There are three list items. As you hover over each one, the class .activeSlide is added. So when each list item has the .activeSlide class, it needs to show the array value corresponding to its index number... Does that make sense? For example, when I hover over "Title 1", a second "Title 1" should appear next to it, but not the other two values.
@ElliPetersen do you add the class in a different hover?
It's added by the jQuery Cycle plugin depending on which slide is active. I've updated the OP with the code I'm trying to get to work, but returns UNDEFINED on hover.
|

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.