3

I'm totally new to web dev and trying to learn and practice Javascript. I've learned a trick to change displayed image by changing the src of the image (they are named: pic-0.jpg, pic-1.jpg, pic-2.jpg... so on) but somehow my code won't work. Anyone can have a look at my codes, help me out and give me some advice. I'd appreciate it!

Here are the codes:

var counter = 0;

document.getElementById('btn-next').addEventListener('click', changeImg);

function changeImg() {
  counter++;
  document.getElementById('picture').src = 'pic-' + counter + '.jpg';
}
<body>
  <section class="container">
    <div class="picture-container">
      <img src="pic-0.jpg" id="picture" alt="">
    </div>
    <button id="btn-previous">Previous</button>
    <button id="btn-next">Next</button>
  </section>
  <script src="r.js"></script>

</body>

cheers and many thanks!

1
  • 2
    getElementById not Elements , see the below modified code Commented Jan 14, 2020 at 11:13

3 Answers 3

2

you are using getElementsById , you should use getElementById because id is always unique and one page can have only one id , you can use getElementsByClassName because class can be attached more than one tag

var counter = 0;

document.getElementById('btn-next').addEventListener('click', changeImg);

function changeImg() {
    counter++;
    document.getElementById('picture').src = 'pic-' + counter + '.jpg';
    document.getElementById('picture').alt = 'pic-' + counter + '.jpg';
}
<body>
    <section class="container">
        <div class="picture-container">
            <img src="pic-0.jpg" id="picture" alt="df">
        </div>
        <button id="btn-previous">Previous</button>
        <button id="btn-next">Next</button>
    </section>
    <script src="r.js"></script>

</body>

//---------------------JavaScript---------------

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

3 Comments

oh sorry for my mistyping! thanks for pointing that out. I already edited it but still didnt work.
its working you can run my snippet and match with your codes , for better understanding m changing the alt attribute
yes it is! also with my codes! it's weird, man. I tried by switching the assignment of the Event to the PREVIOUS button and it worked. I went to the console to check again and saw NO EVENT assigned to the NEXT button. After a while, I closed the JS file window I was working on and reopened it. BOTH worked with the same codes! Thanks anyways!
1

<body>
    <section class="container">
        <div class="picture-container">
            <img src="https://i.picsum.photos/id/1/5616/3744.jpg" id="picture" height=50 width=50 alt="df">
        </div>
        <button id="btn-previous">Previous</button>
        <button id="btn-next">Next</button>
    </section>
    <script>
    var counter = 1;
    document.getElementById('btn-next').addEventListener('click',       nextImg);
     document.getElementById('btn-previous').addEventListener('click',preImg);

function nextImg() {
    counter++;
    document.getElementById('picture').src = 'https://i.picsum.photos/id/' + counter + '/5616/3744.jpg';
}
function preImg() {
    counter--;
    document.getElementById('picture').src = 'https://i.picsum.photos/id/' + counter + '/5616/3744.jpg';
}
    </script>
</body>

1 Comment

this works beautifully, thanks buddy! my codes after a while worked too! thanks!
1

I think the best thing is store all of your images path into an array. Then

var imgArr = ["1.jpg", "2.jpg"]
var counter = 0;

function changeImg(evt) {

  if(evt === 'prv'){
    if(counter === 1)
      counter++;
    else
      counter--;
  }
  else{
     if(counter === (imgArr.length))
        counter = 1;
     else
        counter++;
  }

  document.getElementById('picture').src = imgArr[counter]; 


}




<section class="container">
  <div class="picture-container">
    <img src="" id="picture" alt="">
  </div>
  <button id="btn-previous" onClick="changeImg('prv')">Previous</button>
  <button id="btn-next" onClick="changeImg('nxt')">Next</button>
</section>

1 Comment

I have also read some and this I figured is a better solution. But I just wanted to figure out "WHY THAT DOESNT WORK" because I'm learning and testing. Thanks!

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.