Skip to content

Instantly share code, notes, and snippets.

View takahirohonda's full-sized avatar

Takahiro Honda takahirohonda

View GitHub Profile
@takahirohonda
takahirohonda / better-way-to-attach-event-listener-with-vanilla-js-5-closest-header-nav.js
Last active November 10, 2019 01:36
better-way-to-attach-event-listener-with-vanilla-js-5-closest-header-nav.js
// Instead of calling addEventListerner 3 times...
document.querySelector('.header-right-nav')
.addEventListener('click', e => {
funcForHeaderRightNavClick(e);
})
document.querySelector('.header-left-nav')
.addEventListener('click', e => {
funcForHeaderLeftNavClick(e);
})
document.querySelector('.nav-menu')
@takahirohonda
takahirohonda / better-way-to-attach-event-listener-with-vanilla-js-4-closest.js
Created November 10, 2019 01:27
better-way-to-attach-event-listener-with-vanilla-js-4-closest.js
document.querySelector('.my-form')
.addEventListener('change', (e) => {
if (e.target.closest('.radio-input') !== null) {
console.log(`${e.target.value} is picked`);
}
});
@takahirohonda
takahirohonda / better-way-to-attach-event-listener-with-vanilla-js-3-loop-way.js
Created November 10, 2019 01:22
better-way-to-attach-event-listener-with-vanilla-js-3-loop-way.js
const radioInputs = document.querySelectorAll('.radio-input')
for(let input of radioInputs) {
input.addEventListener('change', e => {
console.log(`${e.target.value} is picked`)
})
}
@takahirohonda
takahirohonda / better-way-to-attach-event-listener-with-vanilla-js-3-loop-way.js
Created November 10, 2019 01:22
better-way-to-attach-event-listener-with-vanilla-js-3-loop-way.js
const radioInputs = document.querySelectorAll('.radio-input')
for(let input of radioInputs) {
input.addEventListener('change', e => {
console.log(`${e.target.value} is picked`)
})
}
@takahirohonda
takahirohonda / better-way-to-attach-event-listener-with-vanilla-js-2-jQuery-way.js
Created November 10, 2019 01:19
better-way-to-attach-event-listener-with-vanilla-js-2-jQuery-way.js
$('.radio-input').change((e) => {
console.log(`${e.target.value} is picked`)
})
@takahirohonda
takahirohonda / better-way-to-attach-event-listener-with-vanilla-js-1.html
Last active November 10, 2019 01:48
better-way-to-attach-event-listener-with-vanilla-js-1.html
<form class="my-form">
<fieldset class="input-field" role="radiogroup" tabindex=0>
<legend class="form-label">Venue Location</legend>
<div class="radio-button-container">
<div class="radio-button-group" tabindex=0>
<input type="radio" class="radio-input" id="sydney"
name="venue" value="Sydney" />
<label class="radio-label" for="sydney">
<span class="ph-tick"></span>
Sydney
@takahirohonda
takahirohonda / replacing-local-storage-with-indexeddb-4-insert-then-read.ts
Created November 9, 2019 12:14
replacing-local-storage-with-indexeddb-4-insert-then-read.ts
export const initialiseDbAndFetchData = async () => {
const initialDataArray = [
initialList,
initialCategories
]
const keyArray = ['list', 'category']
if (checkIndexDbBrowserSupport) {
try {
@takahirohonda
takahirohonda / replacing-local-storage-with-indexeddb-3-clear-database.ts
Created November 9, 2019 12:10
replacing-local-storage-with-indexeddb-3-clear-database.ts
export const clearIndexedDB = (): Promise<string> => {
const req = indexedDB.deleteDatabase('mtd')
return new Promise((resolve, reject) => {
req.onsuccess = () => {
resolve('DB deleted successfully')
}
req.onerror = (e: any) => {
reject(`Error in DB delete: ${e.target.error}`)
}
})
@takahirohonda
takahirohonda / replacing-local-storage-with-indexeddb-3-insert-data.ts
Created November 9, 2019 12:09
replacing-local-storage-with-indexeddb-3-insert-data.ts
export const insertRecordToIndexedDb = (data: any, key: string) => {
let db
const request = indexedDB.open('mtd', 1)
return new Promise((resolve, reject) => {
request.onsuccess = (e: any) => {
db = e.target.result
console.log('opened indexedDb to upsert...', db)
if (db.objectStoreNames.contains('mtd-data')) {
const transaction = db.transaction('mtd-data', 'readwrite')
const store = transaction.objectStore('mtd-data')
@takahirohonda
takahirohonda / replacing-local-storage-with-indexeddb-3-get-data.ts
Created November 9, 2019 12:07
replacing-local-storage-with-indexeddb-3-get-data.ts
const getReadOnlyStore = () => {
// db is defined at the top
const request = indexedDB.open('mtd', 1)
return new Promise((resolve, reject) => {
request.onsuccess = (e: any) => {
db = e.target.result
const transaction = db.transaction('mtd-data', 'readonly')
const store = transaction.objectStore('mtd-data')
resolve(store)
}