How do you detect that the enter key was pressed in two textareas
? I have a way to detect the enter key was pressed in one textarea
and need help with detecting the enter key was pressed in two textareas
.
This is what I've done so far:
$('#test1').keyup(function(e) {
if(e.keyCode == 13) {
$("#GFG_DOWN").text("Enter key pressed inside textarea");
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea id="test1"></textarea>
<textarea id="test2"></textarea>
<br>
<p id = "GFG_DOWN" style =
"color:green; font-size: 20px; font-weight: bold;">
</p>
CodePudding user response:
I guess you must handle this with two FlagVariables.
let text1Flag = false;
let text2Flag = false;
function callbackHandler(e, textType) {
if(e.keyCode == 13) {
if (textType == 1) {
text1Flag = true;
} else {
text2Flag = true;
}
if (text1Flag && text2Flag) {
$("#GFG_DOWN").text("Enter key pressed inside textarea");
text1Flag = false;
text2Flag = false;
}
}
}
$("#test1").keyup((ev) => callbackHandler(ev, 1));
$("#test2").keyup((ev) => callbackHandler(ev, 2));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea id="test1"></textarea>
<textarea id="test2"></textarea>
<br>
<p id = "GFG_DOWN" style =
"color:green; font-size: 20px; font-weight: bold;">
</p>