Javascript Reference - HTML DOM Source Object








The Source object represents an HTML <source> element.

Example

We can access a <source> element by using getElementById().


<!DOCTYPE html>
<html>
<body>
<audio controls>
  <source id="mySource" src="http://java2s.com/style/demo/rain.mp3" type="audio/mpeg">
  <source src="horse.ogg" type="audio/ogg">
</audio><!--from   ww  w .j  av a  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>

The code above is rendered as follows:





Source Object Properties

Property Description
media Sets or gets the media attribute in a <source> element
src Sets or gets the src attribute in a <source> element
type Sets or gets the type attribute in a <source> element

Standard Properties and Events

The Source object supports the standard properties and events.