user3739622
user3739622

Reputation: 19

Uncaught TypeError: undefined is not a function for "onclick="this.firstChild.play()"

Why am I getting "Uncaught TypeError: undefined is not a function" error:

<div class="mainBox">test Text&nbsp;<a onclick="this.firstChild.play()">
     <audio src="sounds/audio1.mp3"></audio>
     <img class="audioPlayer" src="images/play.jpg"></a>
</div>

Upvotes: 0

Views: 1406

Answers (1)

Oriol
Oriol

Reputation: 288100

As @Pointy said, there is a line break and tabulation between <a> and <audio>, so this.firstChild is that text node.

You can fix it using

this.firstElementChild

which will be the first element child instead of the first node child.

Upvotes: 5

Related Questions