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

188
Visualizações
I want to change the image source of a single cell of a table in html
    <tr><!-- 8 -->
            <td id = "8A"><!-- A -->
                <img src="Media/empty_square_white.png" height="160px" width ="160px">
            </td>
    ...
    <script>
        function changeSource(){
        document.getElementById("8A").src="empty_square_brown.png";
        }
    </script>
    <button onclick="changeSource">change</button>

This is what I have tried to do however when I press the button nothing seems to happen. I have also tried to do this without the button however that didn't seem to work either, I have defined the id using internal CSS as well. Thanks

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

Your id references to the surrounding td which has no attribute src. You need to set the id on the img element or reference to the child element.

Also your new value "empty_square_brown.png" is missing the path which is set in the original source "Media/" and the onclick call needs the function brackets. Here some working example. With a title attribute to show the effect.

<tr><!-- 8 -->
            <td><!-- A -->
                <img id="8A" src="Media/empty_square_white.png" height="160px" width ="160px" title="empty_square_white.png">
            </td>
    ...
    <script>
        function changeSource(){
          document.getElementById("8A").src="Media/empty_square_brown.png";
          document.getElementById("8A").title="empty_square_brown.png";
        }
    </script>
    <button onclick="changeSource()">change</button>

about 4 years ago · Santiago Gelvez Relatório

0

You may try this.

function changeImg(){
    document.getElementById("image").src = "https://images.unsplash.com/photo-1581618048854-b2f6f877cef3?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80"
}
img{
  width:50px;
  height:50px;
}
<table border=1>
  <tr>
    <td >
      <img id="image" src="https://images.unsplash.com/photo-1626285094808-143d7bb02f14?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80" alt="" onclick="changeImg()">
    </td>
    <td>
      <img src="#" alt="">
    </td>
  </tr>
</table>

about 4 years ago · Santiago Gelvez 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