• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript New to JavaScript, trying to make an image gallery change photos on click. Only the first image is showing

teelim14

New Coder
JavaScript:
document.addEventListener('DOMContentLoaded', init);

const baseURL = 'images/';
const nxt = document.querySelector('.nxt');
const bck = document.querySelector('.prev');
const slide = document.querySelector('.pic');
const cars = ['images/280z.jpg', 'images/300zx.jpg', 'images/cosworth.jpg', 'images/m3.jpg'];
let index = 1; //specifies that array starts at 1 because page loads with 0

function init() {
  // Set the initial image source to the first image in the 'cars' array
  slide.src = baseURL + cars[index];
}

nxt.onclick = function(e) {
  e.preventDefault(); // ignores event so page wont reload

  // Increment the index to the next image in the array
  index++;

  // If the index is greater than or equal to the length of the array, reset it to 0
  if (index >= cars.length) {
    index = 0;
  }

  // Set the image source to the next image in the array
  slide.src = baseURL + cars[index];
};


HTML:
<h1>Slides!</h1>
<p>I reccommend you to export this pen and use your own images.</p>
<div class="slides">
 
  <figure>
    <img class="pic" src="images/280z.jpg">
  </figure>

  <a class="prev" href="">previous</a>
  <a class="nxt" href="">next</a>
 
</div>


CSS:
h1 {
    text-align: center;
  }
 
  img {
    display: block;
  }
 
  .slides {
    width:400px;
    margin:auto;
  }
 
  .slides figure {
    margin:0;
  }
 
  .slides .nxt {
    float:right;
  }
 
Last edited by a moderator:
Are you sure the image paths are correct?

JavaScript:
slide.src = baseURL + cars[index];
let's assume index is 0, then here you're concatenating baseURL (which is "images/") with cars[0] (which is "images/280z.jpg").
The result will be "images/images/280z.jpg".
Are you sure that's correct? Or is it supposed to be "images/280z.jpg"?
 
Are you sure the image paths are correct?

JavaScript:
slide.src = baseURL + cars[index];
let's assume index is 0, then here you're concatenating baseURL (which is "images/") with cars[0] (which is "images/280z.jpg").
The result will be "images/images/280z.jpg".
Are you sure that's correct? Or is it supposed to be "images/280z.jpg"?
Its actually just "images/280z.jpg" Ive been trying a lot of different things to get my javascript code looking at the right directory. I removed the "images/" portion from all the array items but I'm still stuck with just the picture of the 280z.jpg
 
Hey, for relative path you may use "../From1AboveDirectory" or "/inCurrentDirectory" or if you are on a computer, absolute path including C and stuff like you would have to type in or find to get to a file on like your computer. There is also location for document that you could make your URL (path) based on. Also, I notice you have like images/images/file, so remove images/ in cars all indices.
With it like that and as you specified without second images it seems like it should work.
Can you post updated code?
Are you sure you are getting slide?
do alert(slide) and see if it is null.
To be sure, add a name or an id amd use that if it is null.
X E.
 
Last edited:

New Threads

Buy us a coffee!

300x250
Top Bottom