HTML <source> id Attribute

An id on a <source> tag assigns an identifier to the source.

The identifier must be unique across the page.



An id attribute on a <source> tag.

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

Using id

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

The id allows JavaScript to easily access the <source> element.

It is also used to point to a specific id selector in a style sheet.

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


<source id="identifier" />



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">

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

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

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:

4.0 Jan 2010
3.5 Jun 2009
9.0 Mar 2011
10.5 Mar 2010
4.0 Jun 2009

You may also like

 Back to <source>

Last updated on Sep 30, 2023