0

I am currently creating an image gallery to work on my skills.

I have an array that consists of images that I have from a folder called 'images'.

I have an image that has an id of 'imgsrc' with an empty source on my HTML page, so it's not displaying anything. I would like to be able to have access to the first image in my 'images' folder and display it as the source for my image using Javascript.

Here is what I tried to do:

var images = ['images/1.jpg', 'images/2.jpg', 'images/3.jpg', 'images/4.jpg', 'images/5.jpg', 'images/6.jpg','images/7.jpg', 'images/8.jpg', 'images/9.jpg'];

var imgsrc = document.getElementById('imgsrc').src;

var index = 0;

imgsrc = images[index];

Here is my HTML :

<section class="gallery">

  <img id='imgsrc' src="">

</section>

What should I do ?

I appreciate all responses

5
  • 1
    You have to specifically set the src on the element itself. Changing the variable value will have no effect on the element that you got the initial value from Commented Oct 14, 2017 at 14:28
  • 1
    document.getElementById("imgsrc").src = images[index]; is enough Commented Oct 14, 2017 at 14:36
  • @RohitasBehera no need to put your answer in a comment also. OP gets notified when answers are added. Comments like "check my answer" are just needless noise Commented Oct 14, 2017 at 14:39
  • okay ..removing answers then Commented Oct 14, 2017 at 14:40
  • Keep in mind that the way you actually have this reference stored and the reference relative to that folder. You MIGHT store it in the root images specifically i.e. "/images/myimage.jpg" Commented Oct 14, 2017 at 14:52

4 Answers 4

1

The problem is that imgsrc stores the string value and not a reference to the image src. Edit it directly:

var images = ['images/1.jpg', 'images/2.jpg', 'images/3.jpg', 'images/4.jpg', 'images/5.jpg', 'images/6.jpg','images/7.jpg', 'images/8.jpg', 'images/9.jpg'];

var index = 0;
console.log(document.getElementById('imgsrc').src);
document.getElementById('imgsrc').src = images[index];
console.log(document.getElementById('imgsrc').src);
<section class="gallery">

  <img id='imgsrc' src="">

</section>

Sign up to request clarification or add additional context in comments.

Comments

0
var images = ['images/1.jpg', 'images/2.jpg', 'images/3.jpg', 'images/4.jpg', 'images/5.jpg', 'images/6.jpg','images/7.jpg', 'images/8.jpg', 'images/9.jpg'];

var imgsrc = document.getElementById('imgsrc');

var index = 0;

imgsrc.setAttribute('src',images[index]);

Try to use setAttribute().

1 Comment

That's what I just did, but all I get in the console is 'imgsrc.setAttribute is not a function'
0

Get the element then set its src

var images = ['images/1.jpg', 'images/2.jpg', 'images/3.jpg', 'images/4.jpg', 'images/5.jpg', 'images/6.jpg','images/7.jpg', 'images/8.jpg', 'images/9.jpg'];
var imgsrc = document.getElementById('imgsrc');// get element
var index = 0;
imgsrc.src = images[index];
console.log(imgsrc.src);

Comments

0

Works with Minimum Code !!!

 <!DOCTYPE html>
<html>
<body>

<h2>JavaScript Arrays</h2>

<section class="gallery">

  <img alt="sd" id='imgsrc' src="">

</section>

 

<script>
 
var images = ['http://i65.tinypic.com/zl2rfr.jpg', 'images/2.jpg', 'images/3.jpg', 'images/4.jpg', 'images/5.jpg', 'images/6.jpg','images/7.jpg', 'images/8.jpg', 'images/9.jpg']; 

var index = 0;

document.getElementById("imgsrc").src  = images[index];
 
 
</script>
</body>
</html>

**

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.