Islam Aboamh
Islam Aboamh

Reputation: 83

using JavaScript code to submit a hidden html input using the GET method without using type=”submit”, via an event

in JavaScript technical interview questions I was asked to write a code that submit a hidden html input using the GET method without using type=”submit”, via an event.

Upvotes: 0

Views: 98

Answers (2)

biberman
biberman

Reputation: 5777

You can use the submit() method:

document.querySelector('button').addEventListener('click', function() {
  document.querySelector('form').submit();
});
<form method="get" action="test.php">
  <input type="text" value="test" hidden>
  <button type="button">Go</button>
</form>


If you don't want extra JavaScript you can use an inline event listener. It's bad practice to mix HTML and JavaScript but it works:

<form method="get" action="test.php">
  <input type="text" value="test" hidden>
  <button type="button" onclick="document.querySelector('form').submit();">Go</button>
</form>

Upvotes: 2

Merada
Merada

Reputation: 11

Don't know if I understand your question correct but you can add a click event or similar to the triggerElement and preventDefault inside the callback. Then you can read out the values in the hidden or non hidden fields and submit via xhr.

Upvotes: 1

Related Questions