I have a function that will display content of item prices which can have their order changed from ascending to descending from the click of a button as well as the buttons innerHTML being changed to display text showing what order they are being displayed in. The onload function is called at the top of my HTML code. There is a check inside my function to see in innerHTML of the button (so it changes to the opposite value each time it is clicked) but when i call the function on onload it appears in console as
'Cannot read properties of null (reading 'innerHTML') at ....... at HTMLScriptElement.onload'
JS function:
function myFunc() {
var elem = document.getElementById("selectionButton")
if (elem.innerHTML=="descending") { elem.innerHTML = "ascending "}
else { elem.innerHTML = "descending"}
Onload:
<script src="script.js" onload="myFunc()" ></script>
Buttons:
<button onclick="myFunc()" id="selectionButton" >Ascending</button>