const empty = ''
const uploadSubmit = document.querySelector('#upload-submit')
const form = document.querySelector('#form')
let activeElement
document.addEventListener('keydown', (e) => {
//document.body.insertAdjacentHTML('afterend', document.activeElement)
// Avoid using e.target.classList.contains, if the class changes
// You also have to change the code.
const hasImageTitle = activeElement.hasAttribute('data-image-title')
if (hasImageTitle) {
if (e.key === 'Enter' && hasImageTitle) {
// Click is only for testing purposes
uploadSubmit.click()
e.preventDefault()
// Use `form.submit()` in production as it is less hacky and will be less prone to any propagation issues.
// Submit will submit the form but does not fire the submit event
// form.submit()
return
}
if(e.key === 'Tab') {
currentImageTab = e.target.closest('[data-upload-details-component]')
.querySelector('[data-tab]')
e.preventDefault()
currentImageTab.focus()
}
}
})
let currentImageTab;
document.addEventListener('focus', () => {
activeElement = document.activeElement
if (activeElement.hasAttribute('data-image-title')) {
currentImageTab = activeElement.closest('[data-upload-details-component]')
.querySelector('[data-tab]')
currentImageTab.setAttribute('tabindex', '-1')
} else {
if (currentImageTab) {
currentImageTab = activeElement.closest('[data-upload-details-component]')
.querySelector('[data-tab]')
currentImageTab.removeAttribute('tabindex')
}
}
}, true)
// To test you can borrow @bravo's test example. This is not needed, only to see it in action.
form.addEventListener('submit', e => {
document.body.insertAdjacentHTML('afterend', 'form would submit')
console.log('form would submit', document.activeElement)
e.preventDefault()
})
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.