How to access link with parameter through html or Html5 form?

Asked

Viewed 104 times

2

I have the following html code.

<form action="http://www.meulinkqueseraredirecionado.com/namer/">
      <input type="text" />
      <input type="submit" value="Acessar" />
</form>

I want that as soon as the user click on the "Access" button, after inserting a number in the input "text", he access the link "http://www.meulinkqueseraredirecionado.com/namer/numeroInserido"

How it is done in HTML or HTML5?

1 answer

2


Using Javascript only, you can do so:

document.getElementById('enviar').addEventListener('click', function(e) {
  var param = document.getElementById('param');
  var act = document.getElementById('form');
  act.action += param.value;
  window.open(act.action, '_blank');
  act.action = "http://www.meusite.com/";
});
<form action="http://www.meusite.com/" id="form">
  <input type="text" id="param">
  <input type="submit" id="enviar">
</form>

By clicking the submit button, Javascript will take the value of the parameter field and add it to the attribute value action form.

Note: In the Stackoverflow snippet the form Submit is blocked.

  • Now it’s not redirecting

  • 1

    It was only necessary to add in javascript after the act.action += param.value; , the code to redirect window.open(act.action, '_blank');

  • 1

    @Tiagoferezin after calling the windows.open, you return the action for the initial amount. Cool, liked! Thank you very much for the edition.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.