Gabriel
Gabriel

Reputation: 1

Uncaught TypeError: Cannot set property 'innerHTML' of null using onclick

I am trying to change the 1 to x. I am getting an error. Perhaps there is an easier way.

<html>
<head>
    <title>Test</title>
</head>
<body>
    <div class="container">
        <div id="id1" onclick="changeText(this.id); this.onclick=null;">1</div>
    </div>
  <script src="javascript.js"></script>
</body>
</html>

Javascript:

function changeText(selectedId) {
  document.getElementById("selectedId").innerHTML = "x"
};

Upvotes: 0

Views: 360

Answers (1)

ItsPete
ItsPete

Reputation: 2368

You're trying to get an element that has id="selectedId" & there isn't one.

Remove the quotation marks in your JavaScript so it is just:

document.getElementById(selectedId).innerHTML = "x"

Upvotes: 1

Related Questions