Javascript DOM HTML Source src Property get

Introduction

Return the URL of a media file:

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

Click the button to return the value of the src attribute 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>//from  www. ja va  2  s . co m

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

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

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

</body>
</html>

The src property sets or gets the value of the src attribute in a <source> element.

The src attribute sets the URL of the media file to play.

The src property accepts and returns a String type value.




PreviousNext

Related