change the action of a form depending on the value of an input

0

this is my form:

<form action="" id="comprobante" class="frm-ventas" method="post">
  <input type="text"  name="nombre" id="nombre">
  <input type="hidden"  name="cod" id="cod" value="">
  <button type="submit">
</form>

I need that when the value of the input "cod" is 01 send me to the action="sales / register_sales" or if it is 03 send me to action="sales / register_buy". Is it possible to do that? What would it be like?

    
asked by Fernando Abel Gonzales Ch 08.02.2018 в 16:14
source

1 answer

0
document.getElementById('send').addEventListener('click', (e) => {
  e.preventDefault()

  if(document.getElementById('cod').value == '01')
    formulario.setAttribute('action', 'ventas/registrar_venta')

  if(document.getElementById('cod').value == '03')
    formulario.setAttribute('action', 'ventas/registrar_compra')

  document.getElementById('comprobante').submit()

})
<form action="#" id="comprobante" class="frm-ventas" method="post">
  <input type="text"  name="nombre" id="nombre">
  <input type="hidden"  name="cod" id="cod" value="">
  <button type="submit" id="send">Enviar</button>
</form>
  

Remember that everything is possible with Javascript

    
answered by 08.02.2018 / 17:31
source