2

I'm trying to read values ("img1.jpg", etc.) from a text file on my server and append to a div to create a slide show.

The only catch is I need the first "slide" to have a class of .active applied.

Here's how my text file reads:

enter image description here

Here's the div I want to append to looks like:

 <div class="carousel-inner">
   <!-- slide -->
     <div class="active item slide3 animated fadeInUpBig">
        <img src="img1.jpg" />
     </div>
   <!-- slide -->
     <div class="item slide3 animated fadeInUpBig">
        <img src="img2.jpg" />
     </div>                           
 </div>

Here's what I tried:

$(function () {
var file = "gallery.txt"; // gallery.txt PATH

$('<div />'.load(file, function(data){ // dummy DIV to hold data 
    var line      = data.split('\n');
    var NofImages = line.length -1;        
    for( i=0; i < NofImages; i++ ){
        image = './cms/data/img/gallery/homepage/'+ line[i].split('|')[2];
        $(".carousel-inner").append("<div class='active item slide3 animated fadeInUpBig'><img src='+ image +' /></div>");
    }   
}

});
user1040259
  • 6,369
  • 13
  • 44
  • 62

3 Answers3

2
$(function () {
    var file = "gallery.txt"; // gallery.txt PATH

    $('<div />').load(file, function(data){ // dummy DIV to hold data 
        var line      = data.split('\n'),
            NofImages = line.length -1,
            imageURL  = '';
        for( i=0; i < NofImages; i++ ){
            imageURL = './cms/data/img/gallery/homepage/'+ line[i].split('|')[2];
            $(".carousel-inner").append("<div class='item slide3 animated fadeInUpBig'><img src='"+ imageURL +"' /></div>");
        } 
        // NOW ADD THE .active TO FIRST ONE
        $('.carousel-inner').find('.item').eq(0).addClass('active');
    });
});

Difference between $.ajax() and $.get() and $.load()

Community
  • 1
  • 1
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
1

so add a class if you are at zero

var activeClass = i==0 ? "active " : "";
$(".carousel-inner").append("<div class='" + activeClass + "item...
epascarello
  • 204,599
  • 20
  • 195
  • 236
1

Try this:

$.get("gallery.txt").done(function(response) {
    var lines = response.split("\n");
    for (var i = 0; i < lines.length; i++) {
        var fragments = lines[i].split("|");
        var html = "<div class='item animated fadeInUpBig slide3'><img src='" + fragments[2] + "' /></div>";
        $(".carousel-inner").append(html);
    }

    $(".carousel-inner .item").eq(0).addClass("active");
});
Lukas
  • 9,765
  • 2
  • 37
  • 45