0
although when i click button the image will change then i want image change more than two times how could i do that?
There is an image when I click the button this is change at once. If Want more than twice or three times how could I do. https://code.sololearn.com/W9MK522mDmem/?ref=app https://code.sololearn.com/WDEVj5687OQ7/?ref=app
6 Answers
+ 3
Put those image source in array ,on click move across array and use the value at a particular index and assign it to src attribute of element img
hopefully this helps ,or otherwise wait for someone to code an example for you
+ 2
Prince Gautam here is the working demo of your code.
Click on photo1 button and see the changes.
https://code.sololearn.com/WFbmfltkQJUf/?ref=app
+ 2
Thank you
0
Ok thanks 😊
0
This code might work for you
function changePhoto(elementId, ...args){
if(typeof elementId !== 'string') return console.log("error")
const el = document.getElementById(elementId)
if(!el) return console.log("error")
var srcIndex = el.getAttribute('data-src-index')
if(srcIndex === null){
el.setAttribute('src', args[0])
el.setAttribute('data-src-index', 0)
}else{
srcIndex = parseInt(srcIndex) + 1
if(srcIndex >= args.length){
srcIndex = 0
}
el.setAttribute('src', args[srcIndex])
el.setAttribute('data-src-index', srcIndex)
}
}
function loadphoto1(){
changePhoto(
//tag id
'img1',
//Image urls separated by coma
"https://images.pexels.com/photos/4058719/pexels-photo-4058719.jpeg?cs=srgb&dl=pexels-streetwindy-4058719.jpg&fm=jpg",
"https://images.pexels.com/photos/753500/pexels-photo-753500.jpeg?cs=srgb&dl=pexels-barry-plott-753500.jpg&fm=jpg")
}
0
No it's not working