How to submit form by pressing enter or pressing submit button?
Solution 1:
Instead of using <input type="button" />
, use <input type="submit" />
to send the form.
The enter button should submit the form by default.
Solution 2:
You should attach event listener to textarea
tag and listen for keypress
and when Enter
is pressed you invoke your function:
var textarea = document.querySelector("textarea");//get textarea tag
//NOW replace this with: var input = document.getElementById("myTextarea")
//BELOW change textarea with input
textarea.addEventListener("keypress", function(e){
console.log(e.which, e.target.value);
if(e.which === 13)//code number for enter key
myFunction(e.target.value);//run function with value
});
function myFunction(val) {
var testThis = document.getElementById("myTextarea").value;
if ( testThis.indexOf("launch") > -1 ) {
window.location = 'http://www.cateto.weebly.com/benoit.html';
return false;
}
}
<form method="POST" id="form01">
<textarea name="inputBox123" id="myTextarea" style="white-space:nowrap;">
</textarea>
<!-- change above with input in the comment I entered -->
<input type="submit" value="Submeter" onclick="myFunction()" />
</form>