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

171
Visualizações
How to get the form action on submit when using the formaction attribute on a button

A <form> has different buttons. On the submit event I would like to know what button was clicked. I had the idea that the formaction attribute on the button could be used for this. On MDN it says that the formaction attribute:

Overrides the action attribute of the button's form owner.

But when getting the form action this value does not change according to the formaction. So how can I get the value of formaction or do you have any other suggestions for solving this?

document.forms.form01.addEventListener('submit', e => {
  e.preventDefault();

  console.log(e.target.action);

  switch (e.target.action) {
    case 'a':
      // do "a" stuff
      break;
    case 'b':
      // do "b" stuff
      break;
  }
});
<form name="form01" action="default">
  <button formaction="a">A</button>
  <button formaction="b">B</button>
</form>

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Check out e.submitter. it'll give you the element that submitted the form

about 4 years ago · Juan Pablo Isaza Relatório

0

Add value to button and then use submitter as below.

document.forms.form01.addEventListener('submit', e => {
  e.preventDefault();

  console.log(e.submitter.value);

  switch (e.submitter.value) {
    case 'a':
      // do "a" stuff
      break;
    case 'b':
      // do "b" stuff
      break;
  }
});
<form name="form01" action="default">
  <button value="a">A</button>
  <button value="b">B</button>
</form>

about 4 years ago · Juan Pablo Isaza Relatório

0

If a formaction is attached to button to override submit event then you need to grab the element which have current focus after submit event is sent therefore to grab active element value use document.activeElement

document.forms.form01.addEventListener('submit', e => {
  e.preventDefault();
  
  let action = document.activeElement.getAttribute("formaction");

  console.log(action);

  switch (action) {
    case 'a':
      // do "a" stuff
      break;
    case 'b':
      // do "b" stuff
      break;
  }
});
<form name="form01" action="default" method="get">
  <button formaction="a">A</button>
  <button formaction="b">B</button>
</form>

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