Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How do I use Javascript to loop or cycle through and array of images on one increment per click?

Participant ,
Sep 10, 2023 Sep 10, 2023
6.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 6 Correct answers

Community Expert , Sep 10, 2023 Sep 10, 2023

Here is a basic of what I understood :

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Swap images</title>
</head>
<body>
    <button id="myButton">Swipe</button>
    <img id="myImg" src="" alt="Image">
    
    <script>
        // Here you create your array
        const images = [
            'a-one.jpg',
            'a-two.jpg',
            'a-three.jpg',
            // add all images th
...
Translate
LEGEND , Sep 10, 2023 Sep 10, 2023

You already have a good reply as a solution but I'll throw this one into the mix just incase arrays like the following = const images = ['blah' , 'blah' , 'blah'] are a bit daunting to you.

 

You can still hard code the images into the html as you may usually do and then use css to hide them and javascript to create an array from them, just add your own images, instead of image_1.jpg, image_2.jpg etc:

 

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Click Through Images</titl
...
Translate
LEGEND , Sep 18, 2023 Sep 18, 2023
quote

How do we attach an event listener to the image object? and get the image carousel to scroll backward? The demo video is attached along with the coding.

 

By AmySykes

 

Add the below javascript directly before the closing </script> tag:

 

images.forEach(function(img) {
img.onclick = function() {
images.forEach((img) => {
img.classList.remove('active')
})
if(count === 0) {
count = 0;
}
else {
count--;
}
images[count].classList.add('active');
}
})
Translate
Community Expert , Sep 19, 2023 Sep 19, 2023
Translate
Community Expert , Sep 19, 2023 Sep 19, 2023

All you have to do is google the subject, and you'll find tons of tutorials on the web. they're all pretty good, but it mostly depends on the entry level, the level covered, and the need to put it into practice at the same time, otherwise it remains mainly theoretical.

 

Regarding the basics, the link indicated by @BenPleysier can only be confirmed, MDN is a real source for beginners and experts alike, a must have!

 

As a complement, you can also start with :

...
Translate
LEGEND , Oct 02, 2023 Oct 02, 2023
quote

Happy Monday!!

If this was written out the long way without arrow functions how would that look?

images.forEach(function(img) {
img.onclick = function() {
images.forEach((img) => {
img.classList.remove('active')
})
if(count === 0) {
count = 0;
}
else {
count--;
}
images[count].classList.add('active');
}
})


By AmySykes

 

 

NOT a great deal of difference:

 

NO ARROW FUNCTION VERSION:

 

images.forEach(function(img) {
img.onclick = function() {
images.forEach(function(img) {
img.classList.remove('active')
})
if(count === 0) {

...
Translate
LEGEND ,
Sep 18, 2023 Sep 18, 2023
quote

It WORKS!!! Thank you so much.♥♥♥


By AmySykes

 

Wonderful. Learning a bit of javascript certainly opens up all kinds of possibilities.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 19, 2023 Sep 19, 2023

Hi Osgood, Happy Tuesday. I am struggling with understanding and learning the arrow function. I am using w3school.com to try and learn it. Do you have any other suggestions on learning resources?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 19, 2023 Sep 19, 2023

See JavaScript — Dynamic client-side scripting - Learn web development | MDN (mozilla.org)

Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 19, 2023 Sep 19, 2023

Hi Ben, Thank you for the link learning Resource.  Wrapper? I have never heard of it. I'll look into it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 19, 2023 Sep 19, 2023
quote

Hi Osgood, Happy Tuesday. I am struggling with understanding and learning the arrow function. I am using w3school.com to try and learn it. Do you have any other suggestions on learning resources?


By AmySykes

 

Arrow function is just a new es6 fancy way of writing a javascript function or looping through an array. I actually prefer the old syntax which you can still use.

 

images.forEach((img) => {

// do someting

})

 

Is the same as:

 

images.forEach(function(img) {

 // do something

})

 

Similarly you can write a function like:

 

function swapImg() {

// do something

}

 

Or using the arrow function:

 

 const swapImg = () =>  {

// do something

}

 

Personally l think the arrow function is not very distinctive within the code block and can get mixed up with declaring ordinary variables. I use both methods and often mix and match, both work.

 

I think arrow functions resolved some kind of function scoping issues where by the 'this' javascript keyword wasn't recognise using the old function() { } method, without jumping through hoops.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 19, 2023 Sep 19, 2023

All you have to do is google the subject, and you'll find tons of tutorials on the web. they're all pretty good, but it mostly depends on the entry level, the level covered, and the need to put it into practice at the same time, otherwise it remains mainly theoretical.

 

Regarding the basics, the link indicated by @BenPleysier can only be confirmed, MDN is a real source for beginners and experts alike, a must have!

 

As a complement, you can also start with :

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 21, 2023 Sep 21, 2023

Hi Lenna, Happy Thursday. Thank you so much for the useful information. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 02, 2023 Oct 02, 2023

Happy Monday!!

If this was written out the long way without arrow functions how would that look?

images.forEach(function(img) {
img.onclick = function() {
images.forEach((img) => {
img.classList.remove('active')
})
if(count === 0) {
count = 0;
}
else {
count--;
}
images[count].classList.add('active');
}
})

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 02, 2023 Oct 02, 2023
quote

Happy Monday!!

If this was written out the long way without arrow functions how would that look?

images.forEach(function(img) {
img.onclick = function() {
images.forEach((img) => {
img.classList.remove('active')
})
if(count === 0) {
count = 0;
}
else {
count--;
}
images[count].classList.add('active');
}
})


By AmySykes

 

 

NOT a great deal of difference:

 

NO ARROW FUNCTION VERSION:

 

images.forEach(function(img) {
img.onclick = function() {
images.forEach(function(img) {
img.classList.remove('active')
})
if(count === 0) {
count = 0;
}
else {
count--;
}
images[count].classList.add('active');
}
})

 

 

ARROW FUNCTION VERSION:

 

images.forEach((img) => {
img.onclick = () => {
images.forEach((img) => {
img.classList.remove('active')
})
if(count === 0) {
count = 0;
}
else {
count--;
}
images[count].classList.add('active');
}
})

 

 

 

YOU COULD SHORTEN THE CODE MORE BY USING THE TERNARY OPERATOR (IN BOLD) AS A REPLACEMENT FOR the IF/ELSE BLOCK:

 

images.forEach(function(img) {
img.onclick = function() {
images.forEach(function(img) {
img.classList.remove('active')
})
count === 0 ? count = 0: count--;
images[count].classList.add('active');
}
})

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 02, 2023 Oct 02, 2023
LATEST

Thank-you. Have a happy night.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines