Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

163
Vistas
Onclick doesn't call function in php echo for loop

I created a loop of div's as image thumbnails that can be clicked and every div gets an onclick method while the for loop but if i click on the thumbnails, it doesn't call my function test().

I don't know where the failure is.

I would be very happy for every help.

is there any solution to do this?

i am sitting since hours on that problem but without success.

Thanks a lot for helps!

<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css">
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.bundle.min.js"></script>
    
 
</head>



<style>
.album-container {
  display: flex;
  flex-wrap: wrap;
  background-color: white;
}

.album-container > div {
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  width: 115px;
  height: 115px;
  margin: 10px;
  cursor: pointer;
  border:1px solid lightgray;
}  
    .album-container > div:hover{
        opacity: 0.7;
        transition-duration: 0.5s;
        width: 120px;
        height: 120px;
    }
</style>

<script> 
function test(){
    alert("HELLO");
}
</script>



<body>
<div class="container album-container">
    <?php
    $path    = 'pics/gallery';
    $files = scandir($path);
    $files = array_diff(scandir($path), array('.', '..'));
    foreach($files as $file){
     echo  '<div class="photo_thumb" id="'.$file.'" onclick="test()"  style="background-image:url(pics/gallery/'.$file.')"></div>';
    }    
    ?>
</div>
    

</body>
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda