HTML <source> id Attribute

The id attribute on a <source> tag assigns an identifier to the media or image source element. The identifier must be unique across the page.

Example

#

An id attribute on a <source> tag.

<audio controls>
  <source id="epic-music" src="/media/epic.mp3" type="audio/mpeg">
</audio>

Using id

The id attribute assigns an identifier to the <source> element.

The identifier must be unique across the page.

The id allows programmatic access to the <source> element.

Tip:  id is a global attribute that can be applied to any HTML element.


Syntax

<source id="identifier" />

Values

#

Value Description
identifier A unique alphanumeric string. The id value must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens (-), underscores (_), colons (:), and periods (.).

More Examples

A <source> tag with a unique id.
Clicking the button displays the src attribute value of the element.



<audio controls>
  <source id="mysource" src="/media/epic.mp3" type="audio/mpeg">
</audio> 

<br /><br />
<button onclick="show();">Show source src</button>

<script>
  let show = () => {
     let element = document.getElementById("mysource");
     alert("Src = " + element.src);
  }
</script>

Code explanation

The id attribute assigns a unique identifier for the <source>.

Clicking the button calls JavaScript which locates the <source> using the id.

Finally, the src value of the <source> is displayed in an alert box.


Browser support

Here is when id support started for each browser:

Chrome
4.0 Jan 2010
Firefox
3.5 Jun 2009
IE/Edge
9.0 Mar 2011
Opera
10.5 Mar 2010
Safari
4.0 Jun 2009

You may also like

 Back to <source>
Guides