home ~ projects ~ socials

HTML Radio Button Input Type Element Example

Example Switch

Example Script

JavaScript

function logSwitchState(event) {
  let el = event.target
  console.log(`Switch is now ${el.value}`)
}

document.addEventListener("DOMContentLoaded", () => {
  const buttons = 
    document.querySelectorAll('input[name="example_switch"]')
  buttons.forEach((btn) => {
    btn.addEventListener(
      "input", 
      logSwitchState
    )
  })
})
-- end of line --