Hi there! I'm new to coding and just started learning HTML and CSS. My question is: How can I change the color of buttons once they are clicked?
<div class="icon-bar">
<a href="#"><i class="fa fa-search" onclick="myFunction()"></i></a>
<a href="#"><i class="fa fa-envelope"></i></a>
<a href="#"><i class="fa fa-globe"></i></a>
</div>
I've attempted to use an onclick event, but it didn't have the desired effect. This is the function I tried:
<script>
function myFunction() {
document.getElementById("fa fa-search").style.color = "red";
}
</script>
Below is the complete code:
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
body {margin:0}
.icon-bar {
margin-top: 20%;
margin-left: 0.2%;
width: 90px;
background-color: rgba(85, 85, 85, 0.85);;
}
.icon-bar a {
display: block;
text-align: center;
padding: 16px;
transition: all 0.3s ease;
color: white;
font-size: 36px;
}
.icon-bar a:hover {
background-color: rgba(0, 140, 255, 0.5);
}
.active {
background-color: rgba(0, 140, 255, 1);
}
</style>
<body>
<div class="icon-bar">
<a href="#"><i class="fa fa-search" onclick="myFunction()"></i></a>
<a href="#"><i class="fa fa-envelope"></i></a>
<a href="#"><i class="fa fa-globe"></i></a>
</div>
<script>
function myFunction() {
document.getElementById("fa fa-search").style.color = "red";
}
</script>
</body>
</html>