Trying to upload my extension locally on my computer and am obtaining this error "Uncaught TypeError: Cannot set property 'onchange' of null". This is most likely because the window has loaded and the chrome extension is running the script in the background (which it really shouldn't be -- only when the popup is actually loaded [when it is clicked])
Have tried looking up where others have successfully overcome this scenario, though most are in other areas or working with JQuery and not a solution for Javascript.
Here is the code:
window.onload = function() {
var pVal = "";
function func(item) {
if (item == 1) {
pVal = "A";
} else if (item == 2) {
pVal = "B";
} else if (item == 3) {
pVal = "C";
} else if (item == 4) {
pVal = "D";
} else if (item == 5) {
pVal = "E";
} else if (item == 6) {
pVal = "F";
} else if (item == 7) {
pVal = "G";
} else if (item == 8) {
pVal = "H";
} else if (item == 9) {
pVal = "I";
} else if (item == 10) {
pVal = "J";
}
}
document.getElementById("test1").onchange = function() {
func(document.getElementById("test1").value);
}
document.getElementById('buttonId').onclick = function() {
document.getElementById('TestId').innerHTML = pVal;
}
}
<!DOCTYPE html>
<html>
<body>
<select id="test1" class="form-element" name="test1" tabindex="8" required>
<option value="" selected disabled hidden>Select something</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<br>
<button id="buttonId" class="buttonClass">Does it</button>
<h3 id="TestId" class="sClass"></h3>
</body>
<script src="popup.js"></script>
</html>
Appreciate all of your help!