0

I'm trying to understand JS referencing.

<a id="ohnoes" href="https://www.kasandbox.org/programming-sounds/rpg/giant-no.mp3">What does Oh Noes say?</a>`

So I reference the above tag here in this function:

var ohnoesEl = document.getElementById("ohnoes");
var onOhNoesClick = function(e) {
    e.preventDefault();
    var audioEl = document.createElement("audio");
    audioEl.src = "https://www.kasandbox.org/programming-sounds/rpg/giant-no.mp3";
    audioEl.autoplay = "true";
    document.body.appendChild(audioEl);
    };

why does this work:

audioEl.src = "https://www.kasandbox.org/programming-sounds/rpg/giant-no.mp3";

but not this:

audioEl.src = ohnoesEl.href;

1 Answer 1

1

Hi I have just tested your code, and it turns out that href works just fine.

<a id="a1" href="https://www.google.ca">Google</a>
<button onclick="changeAudio(event)">Click Me</>

<script>
var a1 = document.getElementById('a1')

function changeAudio(e){
  e.preventDefault()
  var audio = document.createElement('audio')
  audio.src = a1.href
  document.body.appendChild(audio)
}
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.