flowerflower
flowerflower

Reputation: 337

How can I add text to a entered input value

How can I add a string to the text written by the user of my form?

For example, if he wants to search for "test", my form should submit "site:mysite.de test".

(The name where I'm trying to send the string to is q.)

I tried with

<form action="https://searx.me" method="get" accept-charset="UTF-8">
    <input type="text" name="q" placeholder="Search with Searxes" required>
    <input type="hidden" name="q" value="site:mysite.de">
</form>

How expected only the first value is submitted https://searx.me/?q=test

I would prefer a solution with pure html without javascript.

Upvotes: 0

Views: 48

Answers (1)

bhansa
bhansa

Reputation: 7504

Here is an ugly way to do that, looks like binding ;)

var hidden = document.getElementsByClassName('hidden')[0];

var hidden_attr = hidden.getAttribute('value');

var show = document.getElementsByClassName('show')[0];

function magic(){
  hidden.setAttribute('value', hidden_attr + show.value);
  console.log(hidden.getAttribute('value'));
}
<form action="https://searx.me" method="get" accept-charset="UTF-8">
    <input class="show" onkeyup="magic()" type="text" name="q" placeholder="Search with Searxes" required>
    <input class="hidden" type="hidden" name="q" value="site:mysite.de ">
</form>

Upvotes: 1

Related Questions