I have a list of textbox and button:
<p>Please type Enter to move to the next element</p>
<input id="item-0" class="item" type="text" index="0" /><br/>
<button id="item-1" class="btn-item" type="button" index="1">Confirm</button><br/>
<input id="item-2" class="item" type="text" index="2" /><br/>
<input id="item-3" class="item" type="text" index="3" /><br/>
<input id="item-4" class="item" type="text" index="4" />
My purpose: when I hit Enter on a textbox or button, the next element will be focus on. But with the button, when I hit Enter it will move to the next element and that element continues to fire the keyup event (Enter) and move to the next one.
const selects = document.querySelectorAll('.item');
selects.forEach(el => el.addEventListener('keyup', e => {
if (e.key === 'Enter' || e.keyCode === 13) {
let idx = parseInt(e.currentTarget.getAttribute("index"));
focusToTheNextControl(idx);
}
}));
document.querySelector('.btn-item').addEventListener('click', e => {
let idx = parseInt(e.currentTarget.getAttribute("index"));
focusToTheNextControl(idx);
});
function focusToTheNextControl(idx) {
document.querySelector("#item-" + (idx + 1)).focus();
}
const selects = document.querySelectorAll('.item');
selects.forEach(el => el.addEventListener('keyup', e => {
if (e.key === 'Enter' || e.keyCode === 13) {
let idx = parseInt(e.currentTarget.getAttribute("index"));
focusToTheNextControl(idx);
}
}));
document.querySelector('.btn-item').addEventListener('click', e => {
let idx = parseInt(e.currentTarget.getAttribute("index"));
focusToTheNextControl(idx);
});
function focusToTheNextControl(idx) {
document.querySelector("#item-" + (idx + 1)).focus();
}
<p>Please type Enter to move to the next element</p>
<input id="item-0" class="item" type="text" index="0" /><br/>
<button id="item-1" class="btn-item" type="button" index="1">Confirm</button><br/>
<input id="item-2" class="item" type="text" index="2" /><br/>
<input id="item-3" class="item" type="text" index="3" /><br/>
<input id="item-4" class="item" type="text" index="4" />
I have a list of textbox and button:
<p>Please type Enter to move to the next element</p>
<input id="item-0" class="item" type="text" index="0" /><br/>
<button id="item-1" class="btn-item" type="button" index="1">Confirm</button><br/>
<input id="item-2" class="item" type="text" index="2" /><br/>
<input id="item-3" class="item" type="text" index="3" /><br/>
<input id="item-4" class="item" type="text" index="4" />
My purpose: when I hit Enter on a textbox or button, the next element will be focus on. But with the button, when I hit Enter it will move to the next element and that element continues to fire the keyup event (Enter) and move to the next one.
const selects = document.querySelectorAll('.item');
selects.forEach(el => el.addEventListener('keyup', e => {
if (e.key === 'Enter' || e.keyCode === 13) {
let idx = parseInt(e.currentTarget.getAttribute("index"));
focusToTheNextControl(idx);
}
}));
document.querySelector('.btn-item').addEventListener('click', e => {
let idx = parseInt(e.currentTarget.getAttribute("index"));
focusToTheNextControl(idx);
});
function focusToTheNextControl(idx) {
document.querySelector("#item-" + (idx + 1)).focus();
}
const selects = document.querySelectorAll('.item');
selects.forEach(el => el.addEventListener('keyup', e => {
if (e.key === 'Enter' || e.keyCode === 13) {
let idx = parseInt(e.currentTarget.getAttribute("index"));
focusToTheNextControl(idx);
}
}));
document.querySelector('.btn-item').addEventListener('click', e => {
let idx = parseInt(e.currentTarget.getAttribute("index"));
focusToTheNextControl(idx);
});
function focusToTheNextControl(idx) {
document.querySelector("#item-" + (idx + 1)).focus();
}
<p>Please type Enter to move to the next element</p>
<input id="item-0" class="item" type="text" index="0" /><br/>
<button id="item-1" class="btn-item" type="button" index="1">Confirm</button><br/>
<input id="item-2" class="item" type="text" index="2" /><br/>
<input id="item-3" class="item" type="text" index="3" /><br/>
<input id="item-4" class="item" type="text" index="4" />
I try to delay focus on the next element by using setTimeout in 1sec and it stops. I have no ideal how it works and the right way to stop it. Thanks for you help.
Share Improve this question edited Nov 20, 2024 at 0:05 mykaf 1,4121 gold badge11 silver badges14 bronze badges asked Nov 19, 2024 at 16:02 Qudu TaQudu Ta 32 bronze badges 3 |1 Answer
Reset to default 0This happens when you press enter after you focus the button, which triggers the click event along with the event handler for the next focused input. One way to prevent this is to use the keydown
event with event.preventDefault()
:
const selects = document.querySelectorAll('.item');
selects.forEach(el => el.addEventListener('keydown', e => {
if (e.key === 'Enter' || e.keyCode === 13) {
let idx = parseInt(e.target.getAttribute("index"));
focusToTheNextControl(idx);
e.preventDefault();
}
}));
document.querySelector('.btn-item').addEventListener('click', e => {
let idx = parseInt(e.target.getAttribute("index"));
focusToTheNextControl(idx);
});
function focusToTheNextControl(idx) {
document.querySelector("#item-" + (idx + 1)).focus();
}
<p>Please type Enter to move to the next element</p>
<input id="item-0" class="item" type="text" index="0" /><br/>
<button id="item-1" class="btn-item" type="button" index="1">Confirm</button><br/>
<input id="item-2" class="item" type="text" index="2" /><br/>
<input id="item-3" class="item" type="text" index="3" /><br/>
<input id="item-4" class="item" type="text" index="4" />
If you want to, you can also skip the Confirm button entirely by only changing to keydown
event without event.preventDefault()
const selects = document.querySelectorAll('.item');
selects.forEach(el => el.addEventListener('keydown', e => {
if (e.key === 'Enter' || e.keyCode === 13) {
let idx = parseInt(e.target.getAttribute("index"));
focusToTheNextControl(idx);
}
}));
document.querySelector('.btn-item').addEventListener('click', e => {
let idx = parseInt(e.target.getAttribute("index"));
focusToTheNextControl(idx);
});
function focusToTheNextControl(idx) {
document.querySelector("#item-" + (idx + 1)).focus();
}
<p>Please type Enter to move to the next element</p>
<input id="item-0" class="item" type="text" index="0" /><br/>
<button id="item-1" class="btn-item" type="button" index="1">Confirm</button><br/>
<input id="item-2" class="item" type="text" index="2" /><br/>
<input id="item-3" class="item" type="text" index="3" /><br/>
<input id="item-4" class="item" type="text" index="4" />
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742415833a4439725.html
focusToTheNextControl()
twice. Perhaps you should exclude the button from your keyup listener. – mykaf Commented Nov 19, 2024 at 17:27