- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-06-2024 03:46 PM
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-06-2024 07:32 PM
I suggest that you create an array of all the fields you want to automatically move to, and the loop through each of these and add your event listener to each:
function onLoad() {
var fields = ["asset_tag", "serial_number", "mac_address"];
var last = fields.pop();
fields.forEach(function(field, i) {
var control = g_form.getControl(field);
control.addEventListener("keyup", function() {
var nextField = fields[i+1] || last;
var nextControl = g_form.getControl(nextField);
nextControl.focus(); // or setTimeout(function() {nextControl.focus()}, 100) if you really need the delay
});
});
}
Note:
- It's better practice to use addEventListener() rather than onXYZ DOM properties when adding events so that you don't accidently squabble any preexisting event listeners added via `onkeyup`
- I'm not clear on what the setTimeout() is for, you seem to be treating it as though it's synchronous (when it's really asynchronous, ie: all the code after it in the main script will run before the callback function triggers). I've provided a comment on where you may use it if needed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-06-2024 07:32 PM
I suggest that you create an array of all the fields you want to automatically move to, and the loop through each of these and add your event listener to each:
function onLoad() {
var fields = ["asset_tag", "serial_number", "mac_address"];
var last = fields.pop();
fields.forEach(function(field, i) {
var control = g_form.getControl(field);
control.addEventListener("keyup", function() {
var nextField = fields[i+1] || last;
var nextControl = g_form.getControl(nextField);
nextControl.focus(); // or setTimeout(function() {nextControl.focus()}, 100) if you really need the delay
});
});
}
Note:
- It's better practice to use addEventListener() rather than onXYZ DOM properties when adding events so that you don't accidently squabble any preexisting event listeners added via `onkeyup`
- I'm not clear on what the setTimeout() is for, you seem to be treating it as though it's synchronous (when it's really asynchronous, ie: all the code after it in the main script will run before the callback function triggers). I've provided a comment on where you may use it if needed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-07-2024 05:22 AM - edited ‎02-07-2024 05:42 AM
Thanks for your help with this and putting me on the right path to stay in best practices. I see to add more variables, I just need to add variables to line 2.