Question

I am using the caroufredsel and add the carousel's elements via html:

<ul>
  <li> element 1 </li>
  <li> element 2 </li>
  <li> ... </li>
</ul>

What I wanted to do now ist not to hardcode the elements in html, instead I want the caroufredsel read the items from an javascript array.

Is this possible?

Was it helpful?

Solution

You can use $.each() to loop through the array and append the variable to the ul:

$(document).ready(function() {
    var testArr = ["element 1","element 2","element 3","element 4"];
    var listItem ="";
    $.each(testArr, function(i, val) {
         listItem += "<li>" + val + "</li>";
    });

    $('#myUL').html(listItem);
});

Demo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top