0

I'm trying to build a simple HTML5 music player with JavaScript. I've build a player with this code:

<audio controls="controls">
    <source id="song" src="horse.ogg" type="audio/ogg">
</audio>

I've build a couple of <a> which call function changeSong(n);

function changeSong(n) {
    document.getElementById("song").src = "song" + n + ".mp3";
}

and when I click the <a>'s nothing happens to the document, and it won't work..

The <a>'s:

<a href="#1" onclick="changeSong(1)">Play song 1 </a></br>
<a href="#2" onclick="changeSong(2)">Play song 2 </a>

What do to make it work? ////

I tried just running the JavaScript w/o function and it works. but when it's called from the as a function it doesn't...

5
  • 3
    your function signature is changeSong but you are calling playSong Commented Dec 7, 2012 at 14:27
  • Did you even preview the question if it is readable? Commented Dec 7, 2012 at 14:27
  • Where is the code for playSong() Commented Dec 7, 2012 at 14:28
  • I accidentally called playSong insetad if changeSong, I fixed that but it still doesn't work Commented Dec 7, 2012 at 14:36
  • @user1050389 Step through the code by using a debugger. Commented Dec 7, 2012 at 14:52

4 Answers 4

1

Change:

<a href="#2" onclick="playSong(2)">Play song 2 </a>

With:

<a href="#2" onclick="changeSong(2)">Play song 2 </a>

Is that a typo?


Actually, it works in the console:

document.getElementById("song");
<source id=​"song" src=​"horse.ogg" type=​"audio/​ogg">​
document.getElementById("song").src;
"chrome://newtab/horse.ogg"
document.getElementById("song").src = 'Hi.ogg';
"Hi.ogg"
document.getElementById("song").src;
"chrome://newtab/Hi.ogg"
Sign up to request clarification or add additional context in comments.

1 Comment

I'm not using any console, I'm just writing the code in Notepad++ and using Chrome to see it..
0

You miss the name of the method in the call. This works:

<a href="#1" onclick="changeSong(1)">Play song 1 </a></br>
<a href="#2" onclick="changeSong(2)">Play song 2 </a>

Comments

0

what is your goal ? Do not tell me that you are trying to call the

changeSong(1)

on the "Play song 1" click.

just by reading your code I can say that change

<a href="#2" onclick="playSong(2)">Play song 2 </a>

by

<a href="#2" onclick="changeSong(2)">Play song 2 </a>

Or I'm missing something :/

Can you add some debug code into the

changeSong(n){
    document.getElementById("song").src="song"+n+".mp3";
}

an tell us if something is appening :)

3 Comments

I'm sorry, I'm just using the browser.. I have no Idea how to debug
what king of browser are you using? there is a large amount of addons to help you to debug your js code. For exemple: Firefox firebug Chrome console IE tutorial
Follow this link, you will learn how to debug with Chrome StackOverFlow
0

I'm not familiar with HTML5 , but I can find two errors.

  1. You're calling playSong instead of changeSong

  2. You should declare that changeSong is a function:

    function changeSong(n){
        document.getElementById("song").src = "song" + n + ".mp3";
    }
    

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.