You don’t need to update the value to each keystroke, you can do this only when the form is submitted.
<form id="form-tipologia" action="http://teste.desenvol.br/#/pesquisa/esplanada/" method="GET" class="pesquisa-plataforma">
<div class="form-group mb-4">
<input name="busca-tipologia" href="" type="search" class="form-control" id="busca-tipologia" aria-describedby="" placeholder="Pesquisar dados...">
</div>
</form>
In Javascript it would be:
const form = document.getElementById("form-tipologia");
const input = document.getElementById("busca-tipologia");
form.addEventListener("submit", function () {
this.action += encodeURIComponent(input.value);
});
The use of the function encodeURIComponent
is essential in this case to get around problems that the injected text dynamically break your URL. For example, if it were typed a/b
in the field, in the URL http://localhost/a/b
. The bar here should not belong to path of your URL, it is only part of the content of path, so requires coding, to stay http://localhost/a%2Fb
and thus do not interfere with the URL.
It is worth mentioning that, as the field has the name
, its value will also be sent to query string: http://localhost/a%2Fb?busca-tipologia=a%2Fb
. If you want to avoid this, instead of making the natural submission of the form, you can redirect the user to the page:
const form = document.getElementById("form-tipologia");
const input = document.getElementById("busca-tipologia");
form.addEventListener("submit", function (event) {
event.preventDefault();
window.location.href = this.action + encodeURIComponent(input.value);
});
Thanks, Sam! It worked ! Thanks !
– Thiago lourenço