Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!