I want to make input box
um..
I started JS this week.. why it fires twice? plz help me :)..
if I press 1 , 2 , 3 , Just log will be 1, 2, 3..
const myForm = document.querySelector('#my-form')
const myArea = document.querySelector('#text-A')
myArea.addEventListener('keydown', function(a) {
myForm.addEventListener('input', function(a) {
console.log(a.currentTarget)
})
if (a.which == 13) {
if (!a.shiftKey) {
a.preventDefault();
}
}
})
<html>
<head>
<link rel="stylesheet" href="test.css">
<script defer src="test.js"></script>
</head>
<body>
<form id="my-form">
<textarea type="submit" id="text-A" name="text-A"> </textarea>
</form>
</body>
</html>
2
Answers
You are using
input
event listner insidekeydown
event, Every time keydown event trigger you are registering another event listner.Do not nest your addEventListeners and do not call addEventListener in an event that can repeat, for example when clicking a button.
textarea type="submit"
is also incorrect. You can have<input type="submit"
,<button type="submit"
and <input type="image" which is also a submit, but not<textarea type="submit"
Here is how to submit a form when pressing enter but not shift-enter in a form with only one field