1

I know there are a lot of topics for that but none of them answered my question. I have to print lexicographically smallest and largest property in window. But I got the error Cannot set property of innerHTML of null at solve. Here is my html:

<!DOCTYPE html>
<html lang="en">

<head>
     <title>Loops Homework</title>
     <meta charset="UTF-8">

</head>

<body>
    <span>Problem 1</span>
    <button onclick="solve()">Print</button>
    <div id="answer1">Answer will appear here</div>
    <script src="LexicographicallyProp.js" type="text/javascript"></script>
</body>

</html>

And following javascript file:

function solve() {
let smallest = 'zzz',
    largest = '',
    prop,
    div1 = document.getElementById('#answer1');

for (prop in document) {
    if (prop < smallest) {
        smallest = prop;
    }

    if (prop > largest) {
        largest = prop;
    }
}
div1.innerHTML = 'Smallest property in DOCUMENT: ' + smallest + ' ; Largest prop in DOCUMENT: ' + largest;

}

Kristian Kamenov
  • 307
  • 1
  • 3
  • 11

2 Answers2

1

It should be:

div1 = document.getElementById('answer1');

'#answer1' is the jQuery notation for HTML element IDs.

uncoder
  • 1,558
  • 1
  • 14
  • 19
1

Please change the following line:

div1 = document.getElementById('#answer1');

Use:

div1 = document.getElementById('answer1');