Home > database >  How to show div on Submit Form
How to show div on Submit Form

Time:10-12

Question, trying to show a div when clickon submit

Here is the style I am using (by default display is none)

   display: none;

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.loader {
  border: 16px solid #f3f3f3;
  border-radius: 50%;
  border-top: 16px solid #3498db;
  width: 120px;
  height: 120px;
  -webkit-animation: spin 2s linear infinite; /* Safari */
  animation: spin 2s linear infinite;
}

/* Safari */
@-webkit-keyframes spin {
  0% { -webkit-transform: rotate(0deg); }
  100% { -webkit-transform: rotate(360deg); }
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
</style>
</head>
<body>

<h2>How To Create A Loader</h2>

<div ></div>

</body>
</html>

so, if I need to show the class, I can simply mention it

<div ></div>

My question is, on how to show it when click on the submit button below?

<form action="https://test.domain.com" method="post">
    <input type="submit" value="Upgrade"/>
</form>

Thanks

CodePudding user response:

Try below code:

<form action="https://test.domain.com" method="post" onsubmit={submit()}>
    <input type="submit" value="Upgrade"/>
</form>

function submit(){
   const loader = document.querySelector('.loader');
   loader.style.display = 'block'
}

CodePudding user response:

Add initial css:

#loader: {
  display: none;
}

Remove class from loader div and add Id:

<div id="loader"></div>

HTML Code:

<div id="loader">Loader</div>
<form method="post">
    <input type="button" onclick="handleSubmit()" value="Upgrade"/>
</form>

JS Code used for calling function:

function handleSubmit() {
  document.getElementById('loader').style.display = "block";
  window.location.href = 'https://test.domain.com';
}
  • Related