Skip to content

Commit

Permalink
Automatically focus on the input after searchType selection
Browse files Browse the repository at this point in the history
* Reference: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus
* No need to pass argument for `preventScroll` parameter since
* this is at the top of the page and next to the search input bar.
  • Loading branch information
porobertdev committed Dec 29, 2023
1 parent 323e0d2 commit ab9394d
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const input = document.querySelector('input#search');
// book container
const bookContainer = document.querySelector('div.books.container');
const favBtn = document.querySelector('ul.menu li:nth-child(2)');
const selected = document.querySelector('select');

const userBooks = {
favorites: [],
Expand All @@ -13,13 +14,16 @@ const userBooks = {

input.addEventListener('keydown', handleEvent);
favBtn.addEventListener('click', handleEvent);
selected.addEventListener('click', handleEvent);

function handleEvent(event) {
console.log(event, event.target.localName);
if (event.key == 'Enter') {
api.fetchLibrary();
} else if (event.target.localName == 'li') {
api.showResults(userBooks.favorites);
} else if (event.target.localName == 'select') {
input.focus();
}
}

Expand Down

0 comments on commit ab9394d

Please sign in to comment.