const uploadSubmit = document.querySelector('#upload-submit')
const form = document.querySelector('#form')
let currentImageTab
let activeElement
document.addEventListener('keydown', (e) => {
// Check the input element is the image-title
const hasImageTitle = document.activeElement.hasAttribute('data-image-title')
if (hasImageTitle) {
// The current component
if (e.key === 'Enter') {
/*
** Only using click for testing on SO **
*/
uploadSubmit.click()
e.preventDefault()
/*
Use `form.submit()` instead of `uploadSubmit.click()` as it is
less hacky and less prone to any propagation issues.
Submit will submit the form but will not fire the submit event */
// form.submit()
} else if (e.key === 'Tab') {
// Re-enables tab
e.preventDefault()
currentImageTab.focus()
}
}
})
const getTab = () => document.activeElement.closest('[data-upload-details-component]')
.querySelector('[data-tab]')
document.addEventListener('focus', () => {
// Updates current image tab
if (document.activeElement.hasAttribute('data-image-title')) {
// Each new image title is updated
currentImageTab = getTab()
// This prevents textarea being jumped to
currentImageTab.setAttribute('tabindex', '-1')
} else {
// This re-enables textarea to be focus-able.
// Dosen't have to until the current image title has been accessed
if (currentImageTab) {
currentImageTab.removeAttribute('tabindex')
}
}
}, true)
/*
Just as @bravo's example, this is just for testing, not required
*/
form.addEventListener('submit', e => {
e.preventDefault()
document.body.insertAdjacentHTML('afterend', `<div>Form submitted</div>`)
})
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.