Skip to content
← Back to Community
Little bit of a strange issue.
Profile icon

So, doing a school project, and I'm not supposed to have any errors show up.
But I have one:
TypeError: Cannot set property 'volume' of null at /script.js:3:21
And the weirdest thing is that despite erroring and saying it can't do it, it does do it. I know this because I've tested the output when the line is or isn't commented.
Here's what to look at and in what files:

<audio id="CrySil" autoplay loop src="Media/CrySil.mp3" volume></audio>


var crysilVolume = document.getElementById("CrySil"); crysilVolume.volume = 0.35;

There isn't much that I see that could be wrong, but if you find anything, let me know, because I have to end with zero errors.

Answered by notGilbert [earned 5 cycles]
View Answer
Profile icon
Profile icon
Profile icon

It seems like you are including the script in pages without a #CrySil element

To fix this, remove the script tag from the pages without the element or check if the element exists

check if element exists

var crysilVolume = document.getElementById("CrySil"); if (crysilVolume) { crysilVolume.volume = 0.35 } // do things if exists
Profile icon

@notGilbert bro, that if statement worked. Problem vaporized.
That was way simpler than I thought.

Profile icon

@AstrumDeorum You can click the checkmark next to his name if it helped to reward him