[
{
"src" : "./images/slide-1.png",
"alt" : "Slide one"
},
{
"src" : "./images/slide-2.png",
"alt" : "Slide two"
},
{
"src" : "./images/slide-3.png",
"alt" : "Slide three"
},
{
"src" : "./images/slide-4.png",
"alt" : "Slide four"
}
]
//slide show
var slides = $('.sidenav li');
var slideIndex = 0;
var slideTime = animate();
slideTo(slides[0]);
slides.click(function() {
clearInterval(slideTime);
slideTime = animate();
var selectedIndex = $(this).index();
var slide = slides[selectedIndex];
slideTo(slide);
});
function slideTo(slide) {
slides.removeClass("selected");
$(slide).addClass("selected");
slideIndex = jQuery(slide).index();
}
function animate() {
return setInterval(function() {
var slide = slides[slideIndex];
slideTo(slide)
slideIndex++;
if (slideIndex == slides.length) {
slideIndex = 0;
}
}, 3000);
}
//Json
var image = $('.sidenav li');
$.getJSON('http://localhost:8080/data.json', function(result){
var i = 0;
var j = 0;
console.log(result);
for (j = 0 ; j<result.length ; j++) {
$('.sidenav').append('<li class="selected"><img src="" alt=""></li>');
}
image.each(function(e){
$(this).find('img').attr('src',result[i].src);
$(this).find('img').attr('alt',result[i].alt);
i++;
});
});
<section class="section-slider">
<div class="wrapper">
<div class="content">
<div class="inner-cont">
<div class="title">
<h1 class="black title-section">EXPECT NOTHING ORDINARY</h1>
</div>
<p class="txt black">
Eastern & Oriental Plc is an AIM quoted real estate company, headquartered in the United Kingdom and focused on the development of residential and mixed-use assets in the London and the South East of England.
</p>
<div class="bot-link">
<a href="javascript:;">VIEW OUR COMPANY PROFILE<span class="s s-right_arrow"><span class="hide">arrow</span></span></a>
</div>
<div class="anchor-sign">
<a class="anchor-link" href="#s-three"><span class="s s-next"><span class="hide">arrow</span></span></a>
</div>
</div>
</div>
<div class="slider">
<ul class="sidenav">
<!-- <li class="selected"><img src=" " alt=""></li>
<li><img src=" " alt=""></li>
<li ><img src=" " alt=""></li>
<li><img src=" " alt=""></li> -->
</ul>
</div>
</div>
</section>
I put the code first so now i can write the question. I am trying to make a slideshow. As you can see in the .sidenav
, I have 4 <li>
elements that are commented. They should be added with Jquery and filled with JSON. In the JSON part I am trying to make a <li>
for every img ...thereby 4 elements in this case , and fill them with src
and alt
. But , all I get is empty . Nothing . There are no errors but when I check if something was added , I can see this :
Why is it unknown? Any thoughts ?
As i cannot replicate your exact code and values i made a simple snippet below.
I don't know why you use i++
because the each
function has an index as argument and you can use it.
In the below snippet everything works ( i think ) as you expected.
let result = [{
"src": "./images/slide-1.png",
"alt": "Slide one"
}, {
"src": "./images/slide-2.png",
"alt": "Slide two"
}, {
"src": "./images/slide-3.png",
"alt": "Slide three"
}, {
"src": "./images/slide-4.png",
"alt": "Slide four"
}, {
"src": "./images/slide-5.png",
"alt": "Slide five"
}, {
"src": "./images/slide-6.png",
"alt": "Slide six"
}];
for (i = 0; i < result.length; i++) {
$('.image').append(`<li class="selected"><img src="${result[i].src}" alt="${result[i].alt}"></li>`);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<ul class="image">
</ul>