8

As you can see, I have two different audio files that correspond to id="6.1". Is there a way to use getElementById that calls just the "straight" class of the Id's content? I know you can call options within in Id. I tried "document.getElementById('6.1').options[0].text.play()", but that didn't work. (Obviously, I'm new at this.) Anyone have a hint?

<audio id="6.1" preload='none'>
    <source class="straight" src='audio/6.1.mp3' type='audio/mpeg' />
    <source class="swing" src='audio/swing/6.1.mp3' type='audio/mpeg' />
</audio>
<button onclick="document.getElementById('6.1').play()">&#x25b6;</button>
jennykat
  • 125
  • 7

3 Answers3

4

The . notation is used to denote class selector and it should not be used in the id. So, selector #6.1 will select the element having id as 6 and class 1.

Use querySelector with attribute=value selector.

document.querySelector('[id="6.1"] .straight').classList.add('green');

Demo

document.querySelector('[id="6.1"] .straight').classList.add('green');
.green {
  color: green;
  font-weight: bold;
}
<div id="6.1"> <span class="straight">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fuga, unde.</span>
  <span class="swing">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
        dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</span>
</div>
Tushar
  • 85,780
  • 21
  • 159
  • 179
1

You can use jquery:

$("#6\\.1 .straight")[0]

Or if you insist on plain JavaScript DOM:

document.getElementById("6.1").getElementsByClassName("straight");

However, I don't think this will actually do what you wanted to do. The multiple <source>s are supposed to be fallback audio sources, all containing the same audio just in different formats (e.g. MP3, Ogg). HTML specifies an algorithm that the browser use to negotiate which alternative to use, based on what the browser is capable of playing. You shouldn't need any JavaScript to do the automatic negotiation, the browser should automatically select which alternative to play.

What are you actually trying to do?

Lie Ryan
  • 62,238
  • 13
  • 100
  • 144
  • Thank you! I had tried this, with no success. Unfortunately, when I add change my button code to read ``, the audio file no longer plays. I wonder what I'm doing wrong :) – jennykat Oct 31 '15 at 15:57
  • @jennykat: that's because the `play` method only exists in the ` – Lie Ryan Oct 31 '15 at 16:00
  • I understand about the multiple audio sources for browser compatibility. That's not why I'm setting classes "straight" and "swing." In this case, there are two versions of every audio file—a "straight" version of the rhythm and a "swing" version. (There are 2 audio files, mp3 and ogg, for both straight and swing, so that makes 4 audio files for each rhythm.) Once I get the initial "straight" version of the audio called, I want a user to be able to check a checkbox that lets them hear the swing version upon pressing play. Unchecking the checkbox would return to the straight rhythm. – jennykat Oct 31 '15 at 16:38
  • @jennykat: If you want to pay two distinct sounds, then use two different ` – Lie Ryan Oct 31 '15 at 16:51
0

This is a great fit for document.querySelector() or document.querySelectorAll()

Here is a simple example:

<h1>Div One</h1>
<h1>Div Two</h1>
<script>
    var elements = document.querySelectorAll('h1');
    elements[0].innerHTML = "Div One Works!";
    elements[1].innerHTML = "Div Two Works!";
</script>

Demo

Another advantage is that you can query multiple properties at once. Example:

<div><p>Paragraph in a Div</p></div>
<script>
    document.querySelector('div p').innerHTML = "it works!";
</script>
jaggedsoft
  • 3,858
  • 2
  • 33
  • 41