3

My play() function doesn't get called. I really don't know why.

I have the following code:

var mp = document.getElementById("mp3");
   function play() {
   mp.play();
   console.log("hello");
}

function scan() {
  cordova.plugins.barcodeScanner.scan(
    function(result) {
      if (result.text == "home") {
        var atHomeRepQR = '<div class="container-h"><div class="card"><div class="item item-text-wrap "><i class="icon ion-unlocked"></i> Diese Ansicht ist entsperrt.</div></div><button onclick="play()" class="button button-positive btn">Play</button></div><audio id="mp3"><source src="video/ping.mp3" type="audio/mpeg"></audio>';
        save("atHomeSaveQR", atHomeRepQR);
      }
      if (result.text == "street") {
        var atStreetRepQR = '<div class="container-s"><div class="card"><div class="item item-text-wrap "><i class="icon ion-unlocked"></i> Diese Ansicht ist entsperrt.</div></div><button onclick="play()" class="button button-positive btn">Play</button></div><audio id="mp3"><source src="video/ping.mp3" type="audio/mpeg"></audio>';
        save("atStreetSaveQR", atStreetRepQR);
      }
      if (result.text == "bern") {
        var atBernRepQR = '<div class="container-b"><div class="card"><div class="item item-text-wrap "><i class="icon ion-unlocked"></i> Diese Ansicht ist entsperrt.</div></div><button onclick="play()" class="button button-positive btn">Play</button></div><audio id="mp3"><source src="video/ping.mp3" type="audio/mpeg"></audio>';
        save("atBernSaveQR", atBernRepQR);
      }
      if (result.text == "bahnhof") {
        var atBahnhofRepQR = '<div class="container-ba"><div class="card"><div class="item item-text-wrap "><i class="icon ion-unlocked"></i> Diese Ansicht ist entsperrt.</div></div><button onclick="play()" class="button button-positive btn">Play</button></div><audio id="mp3"><source src="video/ping.mp3" type="audio/mpeg"></audio>';
        save("atBahnhofSaveQR", atBahnhofRepQR);
      }
      if (result.text == "atelier") {
        var atAtelierRepQR = '<div class="container-at"><div class="card"><div class="item item-text-wrap "><i class="icon ion-unlocked"></i> Diese Ansicht ist entsperrt.</div></div><button onclick="play()" class="button button-positive btn">Play</button></div><audio id="mp3"><source src="video/ping.mp3" type="audio/mpeg"></audio>';
        save("atAtelierSaveQR", atAtelierRepQR);
      }

    },
    function(error) {
      alert("Scanning failed: " + error);
    }
  );
}

The syntax

<button onclick="play()" class="button button-positive btn">Play</button></div><audio id="mp3"><source src="video/ping.mp3" type="audio/mpeg"></audio>

is correct when you ask me.

What's my mistake?

sarath
  • 523
  • 7
  • 29
olivier
  • 2,505
  • 6
  • 30
  • 55

3 Answers3

1

It's possible that your mp variable is being initialized before #mp3 is parsed. You should wrap any static code like that in a document.ready block so that its execution is deferred until completion of the page load.

Ideally, this would be achieved using jQuery:

var mp;
$(document).ready(function() {
    mp = document.getElementById("mp3");
});

In it's absence, this will suffice for most modern browsers (see this answer):

var mp;
document.addEventListener("DOMContentLoaded", function(event) { 
    mp = document.getElementById("mp3");
});
Community
  • 1
  • 1
MTCoster
  • 5,307
  • 3
  • 25
  • 46
0

I had to place mp into the function play() like this:

   function play() {
   var mp = document.getElementById("mp3");
   mp.play();
}

Thanks to mplungjan!

olivier
  • 2,505
  • 6
  • 30
  • 55
0

I think, your javascript code is before body tag. Hence in 'mp' element is not getting. So move your javascript code after body tag i.e. after rendering your html.

rishikesh tadaka
  • 473
  • 5
  • 18