elli mccale
elli mccale

Reputation: 206

jQuery append variable array

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();
    });
});

Upvotes: 0

Views: 136

Answers (1)

Spokey
Spokey

Reputation: 10994

$("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

Upvotes: 2

Related Questions