Dofactory.com
Dofactory.com
Earn income with your HTML skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

HTML <source> id Attribute

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

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


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>

Last updated on Sep 30, 2023

Earn income with your HTML skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

Guides