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

128
Visualizações
Img onclick function

I have few images and when clicked, I want to run code, let's say alert image number.

<img onclick="picture(1)" src="asd.jpg">
<img onclick="picture(2)" src="sdf.jpg">
<script>
   function picture(picnumber){
   alert(picnumber);
   }
</script> 

It worked, when I was alerting string, not variable.

Edit: I'm sorry for including example and not my actual code, I thought it was ok, solution given below however didn't fix my code:

<body>
    <style>
        img{
            height:80px;
        }
    </style>
    <script>
        function picture(picbumber){
            alert(picnumber); 
        }
    </script>
    <h1>GALLERY</h1>
    <div class="photos">
        <img onclick="picture(1)" src="https://i.insider.com/5f5a5f37e6ff30001d4e8163?width=700"/>
        <img onclick="picture(2)" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSChu52FnNKfSPRTMY8HIXei8leVAMvkURqqQ&usqp=CAU">
        <img onclick="picture(3)" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSIvgVWXKciw_UZIuzVenY4QvmCbDQb43J1lQ&usqp=CAU">
        <img onclick="picture(4)" scr="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSY9xhvbRt8gXsEyO1dOW41asBHrEb2mZkRgQ&usqp=CAU">
        <img onclick="picture(5)" src="https://dkt6rvnu67rqj.cloudfront.net/cdn/ff/T8cy0-640W8sartvA9TWmv08NbGPFxsVvf8gFtBDE08/1577112797/public/styles/600x400/public/media/int_files/elephant_in_tanzania.jpg?h=f507d761&itok=Ei8OXcGi">
        <img onclick="picture(6)" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR1u8AKJBUZdSX7SIHvd8R1SiYyypx7elpstQ&usqp=CAU">
    </div>
</body>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

just change the order, in onclick you are executing a function that is not defined yet otherwise

<script>
   function picture(picnumber){
   alert(picnumber);
   }
</script> 
<img onclick="picture(1)" src="asd.jpg">
<img onclick="picture(2)" src="sdf.jpg">
about 4 years ago · Juan Pablo Isaza Relatório

0

Please add the script code in head section. See my demo here.

<head>
    <script>
        function picture(picnumber){
            alert(picnumber)
        }
    </script>
</head>
<body>
    <img onclick="picture(1)" src="./asd.jpg" />
    <img onclick="picture(2)" src="./sdf.jpg" />
</body>
</html>```
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