Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

178
Visualizações
Greyed out or disable <a>

I have a HTML page that has submitting request every 5 seconds. how can i disable or greyed out the a href in the setTimeoutFunction before the form submits?

<html lang="en">
<head>
    <META content="text/html; charset=utf-8" http-equiv="Content-Type"/>
    <META name="viewport" content="width=device-width"/>
    <script>
      setTimeout(function () {
         // disable <a>
         document.forms.form.submit();
      }, 5000);
    </script>
</head>

<body>
<section class="wrapper">
    <section class="container">
        <section class="content">
            <h1>Title</h1>
            <p>Description</p>
        </section>

        <form id="form" method="post" action="http://toawebsitepost.com">
            <input type="submit" hidden>
        </form>

        <a class="cancel" href="http://toawebsite.com">Cancel</a>
    </section>
</section>
</body>
</html>
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

As CBroe pointed out it's no good just setting pointer-events to none when the anchor can still be accessible by keyboard.

Instead you could add a click listener to the anchor. When the setTimeout callback is executed it changes a variable called isDisabled to true. If the button is clicked before the time out the handler (in this case) logs a message, otherwise we use preventDefault to stop normal anchor behaviour.

const cancel = document.querySelector('.cancel');

cancel.addEventListener('click', handleClick, false);

let isDisabled = false;

function handleClick(e) {
  if (isDisabled) {
    e.preventDefault();
  } else {
    // Normally you would be navigating
    // to a website, but in this example
    // we're just logging a message
    console.log(`Navigating to ${e.target.href}`);
  }
}

setTimeout(function() {
  isDisabled = true;
  cancel.classList.add('disabled');
  document.forms.form.submit();
}, 5000);
.disabled { color: #ababab; cursor: not-allowed; }
<section class="wrapper">
  <section class="container">
    <section class="content">
      <h1>Title</h1>
      <p>Description</p>
    </section>
    <form id="form" method="post" action="http://toawebsitepost.com">
      <input type="submit" hidden>
    </form>
    <a class="cancel" href="http://toawebsite.com">Cancel</a>
  </section>
</section>

about 4 years ago · Juan Pablo Isaza Relatório

0

You can remove your tag and use instead. Using button:

<section class="wrapper">
    <section class="container">
        <section class="content">
            <h1>Title</h1>
            <p>Description</p>
        </section>

        <form id="form" method="post" action="http://toawebsitepost.com">
            <input type="submit" hidden>
        </form>

        <button class="cancel" onclick="navigateToUrl()">Cancel</button>
    </section>
</section>

JS:

navigateToUrl() {
 window.location.href = "http://toawebsite.com"
}

setTimeout(function () {
    // disable <a>
    const button = document.querySelector('.cancel')[0];
    button.disabled = true; 
   document.forms.form.submit();
 }, 5000);

Or if you want to stick to <a> then add a custom CSS class to it to prevent clicks:

setTimeout(function () {
    // disable <a>
    const aTag = document.querySelector('.cancel')[0];
    aTag.classList.add('disable');
   document.forms.form.submit();
 }, 5000);

CSS:

.disable {
  pointer-events: none;
  color: grey;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You could add a special CSS class that will remove default behavior from a link. PLease check the example:

      setTimeout(function () {
         // disable <a>
         document.querySelector('.cancel').classList.add('disabled');
         document.forms.form.submit();
         document.querySelector('.cancel').classList.remove('disabled');
      }, 5000);
      .disabled {
        pointer-events: none;
      }
<section class="wrapper">
    <section class="container">
        <section class="content">
            <h1>Title</h1>
            <p>Description</p>
        </section>

        <form id="form" method="post" action="">
            <input type="submit" hidden>
        </form>

        <a class="cancel" href="http://toawebsite.com">Cancel</a>
    </section>
</section>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda