Utilize the component class swap-js to designate a specific swap component for JavaScript functionality, allowing the swap to function without a checkbox. Toggling the modifier class swap-active on a click event of the swap component facilitates this behavior.
<script>
// Swap elements JS code below
constswapElements= document.querySelectorAll('.swap-js')
// Function to handle click event on swap elements
functionhandleClick(event) {
// Toggle the 'swap-active' class on the clicked swap element
event.currentTarget.classList.toggle('swap-active')
// Get the 'swap-on' and 'swap-off' elements within the current swap element
constswapOn=event.currentTarget.querySelector('.swap-on')
constswapOff=event.currentTarget.querySelector('.swap-off')
// Determine the value based on the presence of 'swap-active' class
constvalue=event.currentTarget.classList.contains('swap-active') ?swapOn.innerHTML:swapOff.innerHTML// Log the value to the console
console.log(`Value: ${value}`)
}
// Iterate through each swap element and add click event listener
swapElements.forEach(swapElement => {
swapElement.addEventListener('click', handleClick)
})
</script>