-1

I have one image slider webpage. In that many image tags are there. So whenever i want to add image in slider, every time i am adding tags in html. Is it possible to do without adding image tag.

<div id="slideshow" class="slideshow">


   <img src="images/1.jpeg" width="620" height="320" />
   <img src="images/2.jpeg" width="620" height="320" />
   <img src="images/3.jpeg" width="620" height="320" />
   <img src="images/4.jpeg" width="620" height="320" />

</div>

If i want to add new image, i have to add new img tag.Is it possible to add images without without <img> in the html??

I have tried with jquery. there i am not getting images from the folder.

  $('<img />')
    .attr('src', 'images/')
    .appendTo('#div3')

How we can solve this problem ?

Jeevan Roy dsouza
  • 653
  • 3
  • 12
  • 32

2 Answers2

1

i think its all ready done in Is there a way to return a list of all the image file names from a folder using only Javascript?

$.ajax({
 url: "http://yoursite.com/images/",
  success: function(data){
    $(data).find("td > a").each(function(){
       // will loop through 
       alert("Found a file: " + $(this).attr("href"));
   });
 }
 });
Community
  • 1
  • 1
rjdmello
  • 865
  • 2
  • 9
  • 14
0

Utilizing the jQuery library.

You can use the following:

$("#slideshow").append("<img src=\"images/5.jpeg\" />")

Or the following (better readability)

var newImg = $("<img />").attr("src","images/5.jpeg").css({"width":"620px","height":"320px"})
$("#slideshow").append(newImg);

Now to retrieve a file listing of the images in your directory dynamically you will need some sort of server side language (php, ruby, python, c#, nodejs, etc) as Javascript cannot access the file system of the server. Without a server side language you could look into create a JSON or XML file that is then read by your Javascript so that you are not changing html but adding new items quickly.

{"images": [
  {"src" : "images/5.jpeg",
   "css": {
         "width" : "620px",
         "height" : "320px"
    }
  }, {
   "src" : "images/6.jpeg",
   "css": {
         "width" : "620px",
         "height" : "320px"
  }
]}

Then do something like the following to retrieve the json file you created:

$.getJSON("images.json",function(data){
  $.each(data,function(i,image){
    var newImg = $("<img />").attr("src",image.src).css(image.css);
    $("#slideshow").append(newImg);
  });
});

This is untested code and could use a few tweaks to get working.

Resources to take a look at as well to get more information.

stackoverflow: jquery loop on Json data using $.each

jquery-api: http://api.jquery.com/jquery.getjson/

Community
  • 1
  • 1
elephino
  • 13
  • 5