I created a spinning animation in CSS, but now I want to hide it and only display it when a button is clicked. I've tried to achieve this using the following code, but the spinner doesn't appear when I click the submit button.
To hide the spinner initially, I used the CSS property display:none. Then, I attempted to use JavaScript to change the display property to block when the button is clicked, but it's not working as expected.
CSS:
* {
margin: 0;
padding: 0;
}
.loader {
display: none;
top: 50%;
left: 50%;
position: absolute;
transform: translate(-50%, -50%);
}
.loading {
border: 2px solid #ccc;
width: 60px;
height: 60px;
border-radius: 50%;
border-top-color: #1ecd97;
border-left-color: #1ecd97;
animation: spin 1s infinite ease-in;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
JavaScript:
function spinner() {
document.getElementsByClassName("loader").style.display = "block";
}
HTML:
<button type="submit" class="sbtn btn btn-secondary btn-c"
onclick="spinner()" >Log in</button>
I'm still learning JavaScript, so any advice or guidance would be greatly appreciated.