Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

85
Visualizações
How i can change img source ? souppose i have 10 images to change on same source

I want to make a button that simply change photo src from given src of image

<div id="main_photo">
<img class="card-img-top" src="/img/new0.JPG" alt="Avatar" >
</div>

I have new1.JPG to new6.JPG photo which should be changed on this image source on click of a button All photos are in my /img/ folder And here is my HTML code for the button to change src with onclick.

<button id="changeimg" > Change </button>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

As @Teemu said (in comments), if you have the list of the new images you want to change to, you can do it by js code.

<div id="main_photo">
<img class="card-img-top" id="mainImage" src="/img/new0.JPG" alt="Avatar" >
</div>
<button id="changeimg" onclick="changeImage()"> Change </button>
<script>
var imageIndex = 0;
var imageCount = 6; // Can be also an array of images names ["/img/new0.JPG", "/img/new1.JPG", ..., "/img/new6.JPG"]

function changeImage() {
 var imageContainer = window.getElementById("mainImage");
 imageContainer.src = "/img/new" + (imageIndex++) + ".JPG";
 // Make sure you validate the counter so it won't overflow
}
</script>
about 4 years ago · Juan Pablo Isaza Relatório

0

I have a demo for you if you have the list of images. And I believe that you can have that by simple code to get all images in your local folder.

const listSources = [
  '/img/new0.JPG',
  '/img/new1.JPG',
  '/img/new2.JPG',
  '/img/new3.JPG',
  '/img/new4.JPG',
  '/img/new5.JPG',
  '/img/new6.JPG',
]

var currentIndex = 0;
$('#changeimg').click(function() {
    // increase index to show the next image
    currentIndex++;
    // reset if reach the last image
    if (currentIndex == listSources.length) currentIndex = 0;
    // set image src
    $('#main_photo>img').first()
      .attr('src', listSources[currentIndex])
      .attr('alt', listSources[currentIndex]); // change alt to show changes in demo
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="changeimg">Change</button>
<div id="main_photo">
  <img class="card-img-top" src="/img/new0.JPG" alt="/img/new0.JPG">
</div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda