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

337
Views
Play audio onclick (when I click on an image)

I have a school project and I've run into a issue. I made a website that contains simple table, searchable with AJAX. I have image of the person in one column and audio in the second one. What I want to do is when I click on the image it plays audio, and I've successfully done that. The issue is that it's only playing first audio, no matter what picture I click on. So if I click on image in line 5, it will play audio from line 1, and not from line 5 as it should. Click to see how the table looks.

This is part of the code in php:

while($redak = mysqli_fetch_array($rezultat)) {
echo "<tr>";
  echo "<td>" . $redak['id'] . "</td>";
echo "<td>" . $redak['ime'] . "</td>";
echo "<td>" . $redak['prezime'] . "</td>";
echo "<td>" . $redak['adresa'] . "</td>";
echo "<td> <img onclick=\"play()\" src=\"Slike/" . $redak['slika1'] . "\"</td>";
  echo "<td> <audio controls id=\"audio\" src=\"Audio/" . $redak['slika'] . "\"</td>";
echo "</tr>";

This is code for audio play onclick in javascript:

function play() {
    var audio = document.getElementById("audio");
    audio.play();
  }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

There's more than one issue here. First, you never close the <img> and <audio> tags. The second issue is that you're reusing the same id on all the audio tags, which is invalid in HTML. id's must be unique within a document.

I would also recommend not echoing blocks of HTML like you're doing since it makes it harder to spot issues with the HTML.

Change your loop like this:

<?php
while($redak = mysqli_fetch_array($rezultat)) 
{
    $id = 'audio_' . $redak['id'];
?>
    <tr>
        <td><?= $redak['id'] ?></td>
        <td><?= $redak['ime'] ?></td>
        <td><?= $redak['prezime'] ?></td>
        <td><?= $redak['adresa'] ?></td>
        <td><img onclick="play('<?= $id ?>')" src="Slike/<?= $redak['slika1'] ?>" /></td>
        <td><audio controls id="<?= $id ?>" src="Audio/<?= $redak['slika'] ?>" /></td>
    </tr>
<?php
}

Then change your JS to:

function play(id) {
    document.getElementById(id).play();
}
about 4 years ago · Juan Pablo Isaza Report

0

Your id seems to be constant in your loop, so all your audio tags currently have the same id,

you could do something like:

    $id = "audio";
    $id .= $redak['id'];
    echo "<td> <img onclick=\"play($id)\" src=\"Slike/" . $redak['slika1'] . "\"</td>";
    echo "<td> <audio controls id=\"$id\" src=\"Audio/" . $redak['slika'] . "\"</td>

then your javascript function would be:

 function play(id){
  var audio = document.getElementById(id);
  audio.play();
}
about 4 years ago · Juan Pablo Isaza Report

0

The issue seems to be that all created <audio> tags have the same id, that's why your play function selects the first audio file and play it...

you can modify both of your <img> and <audio> tag inside while statement to something like this:

while($redak = mysqli_fetch_array($rezultat)) {
    // ....

    echo "<td> <img onclick=\"play('audio_" . $redak['id'] . "')\" src=\"Slike/" . $redak['slika1'] . "\"</td>";

    echo "<td> <audio controls id=\"audio_" . $redak['id'] . "\" src=\"Audio/" . $redak['slika'] . "\"</td>";
    // ....
}

also you have to change play() function to receive the audio id

function play(audio_id) {
    var audio = document.getElementById(audio_id);
    audio.play();
}
about 4 years ago · Juan Pablo Isaza Report
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!