Javascript DOM HTML Source Object get

Introduction

The Source object represents an HTML <source> element.

We can access a <source> element via document.getElementById():

var x = document.getElementById("mySource");

Click the button to get the URL of the media file.

View in separate window

<!DOCTYPE html>
<html>
<body>

<audio controls>
  <source id="mySource" src="sound.mp3" type="audio/mpeg">
  <source src="sound.ogg" type="audio/ogg">
Your browser does not support the audio element.
</audio>// w  w w .  j ava  2  s .  c  o m



<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {
  var x = document.getElementById("mySource").src;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>



PreviousNext

Related