You can call multiple event listeners in one function by separating them with a space.
In this case we're listening for both the keypress
and click
events, and passing in the event parameter to listen for which button was entered.
<input type="submit" id="submit-btn">Submit</button>
$(function(){
$("#submit-btn").on("keypress click", function(event){
var formValue = $("textarea").val();
if (event.which === 13 || event.type === "click") {
// Form submission code goes here
}
});
});
You could also do it with two functions. Inside the second one, you could ivoke the submit button's click event.
$("#submit-btn").click(function(){
var formValue = $("textarea").val();
// Submission logic goes here.
})
// Trigger the submit button's click event
$("textarea").keypress(function(event) {
if (event.which === 13) {
$("#submit-btn").click();
}
});