Hey Guys
I have a problem using eventlistners on my navbar of the header. I want the links to switch color when I hover on them. The problem I have is that, although I specify ID per link, only 1 link switches color. Can someone help me with my code to alter this so only the link I hover switches (it switches back on mouseout). Here below my relevant code:
HTML header:
<!DOCTYPE html>
<html>
<head>
<title>The Merksem City Running Club</title>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="The Merksem City Running Club" content="">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Montserrat+Subrayada:wght@700&family=Tauri&display=swap" rel="stylesheet">
</head>
<body>
<header>
<nav class="navbar">
<a id='navlink1'>Upcoming Events</a>
<a id='navlink2'>Close Running tracks</a>
<a id='navlink3'>To visit run Area</a>
<a id='navlink4'>Team Results</a>
<a id='navlink5' href='signup.php' >Sign up</a>
<a id='navlink6' href='login.php' >Login</a>
</nav>
<h1 class="pageheader">The Merksem City Running Club</h1>
</header>
Relevant CSS:
/* General + header */
body{
background-color: turquoise;
font-family: 'Tauri', sans-serif;
margin: 0;
}
article{
text-align: center;
}
.navbar{
display: flex;
flex-wrap: wrap;
justify-content: space-around;
position: sticky;
background-color: darkslategray;
color: turquoise;
position: fixed;
top: 0;
width: 100%;
}
a#navlink1, a#navlink2, a#navlink3, a#navlink4, a#navlink5, a#navlink6{
text-transform: uppercase;
font-size: 20px;
color: turquoise;
text-decoration: none;
}
.pageheader{
text-align: center;
background-image: url("Images/dreef.png");
background-repeat: no-repeat;
background-size: cover;
background-position: bottom;
padding: 15% 0;
margin-top: 40%;
}
h1{
font-family: 'Montserrat Subrayada', sans-serif;
color: whitesmoke;
}
.turquoisezone{
height: 25px;
background-color: turquoise;
}
The Javascript I am building so far. As said before, the hovering works but only for 1 link when you hover, and this is the last one always that i added in the JS file.
//JS on the header-links
//navhover1
let link1 = document.getElementById('navlink1');
function colorHover () {
link1.style.color = "red";
}
function colorHoverBack () {
link1.style.color = "turquoise";
}
link1.onmouseover = colorHover;
link1.onmouseout = colorHoverBack;
//navhover2
let link2 = document.getElementById('navlink2');
function colorHover () {
link2.style.color = "red";
}
function colorHoverBack () {
link2.style.color = "turquoise";
}
link2.onmouseover = colorHover;
link2.onmouseout = colorHoverBack;
//navhover3
let link3 = document.getElementById('navlink3');
function colorHover () {
link3.style.color = "red";
}
function colorHoverBack () {
link3.style.color = "turquoise";
}
link3.onmouseover = colorHover;
link3.onmouseout = colorHoverBack;
//navhover4-6
let link4 = document.getElementById('navlink4');
let link5 = document.getElementById('navlink5');
let link6 = document.getElementById('navlink6');
Thanks for your help on this topic. Eventlistners seemed so easy in the beginning
Kind regards,
Yannick