Hi Michael and welcome to StackOverflow.
For this I would recommend you use CSS instead of JS. It’s easier and more efficient that way
#nav_1 li:hover { /* Bold li element on hoverin nav_1 elements */
font-weight: bold;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p>
If you ask I will give you:
</p>
<ul id = 'nav_1'>
<li>Some nice chocolate cake</li>
<li>A fresh-brewed cup of coffee</li>
<li>An honest compliment</li>
<li>A brand new Porsche</li>
</ul>
</body>
</html>
This would be the JS solution. You problem is in the callback. document
does not have a variable called listItems
you might have mistaken it for window
. There was also a typo in getElementByID
it must be getElementById
, JS functions are case sensitive.
<body>
<p>
If you ask I will give you:
</p>
<ul id = 'nav_1'>
<li>Some nice chocolate cake</li>
<li>A fresh-brewed cup of coffee</li>
<li>An honest compliment</li>
<li>A brand new Porsche</li>
</ul>
<script type="text/javascript">
"use strict";
const nav1 = document.getElementById('nav_1');
const listItems = nav1.getElementsByTagName("li");
for(let i = 0;i<listItems.length;i++){
listItems[i].onmouseover = function(e){
listItems[i].style.fontWeight = "bold";
}
listItems[i].onmouseleave = function(e){
listItems[i].style.fontWeight = "normal";
}
}
</script>
</body>