Check if click was triggered by touch in modern browsers

This question is an update to this one, but for modern browsers. (Unfortunately the answers here are outdated and/or not in vanilla JavaScript). Essentially I want to perform a simple test to detect if a click event was triggered by touch or mouse input. The only answer in the aforementioned question that is foolproof and… Read More Check if click was triggered by touch in modern browsers

addEventListener not working for element selected by a new class added from javascript

I’m trying to make the button work when the ratings are clicked. Otherwise I don’t won’t the button addEventListener to work I did check if all the elements are correctly looped through using console.log. I didn’t find any mistakes in my code Code “use strict”; const btn = document.querySelector(“.btn”); const activeBtn = document.querySelector(“.btn–active”); const cardFront… Read More addEventListener not working for element selected by a new class added from javascript

Add Event Listener is not working, i.e. the thing I want add event listener to do is not happening when I click what I should click

I want to add another row to my table when I click a button, but when I do click the button, nothing happens. I do not know if I am using addeventlistener incorrectly or if it is because of other errors in my javascript. Here is my code: HTML <body> <p style="text-align: center;"> hello </p>… Read More Add Event Listener is not working, i.e. the thing I want add event listener to do is not happening when I click what I should click

I have a piece of code that does not work as expected, need an explanation

import React,{useEffect,useState} from ‘react’; export function App(props) { let [state,setState]=useState(2); console.log(‘outside’,state); useEffect(()=>{ document.querySelector(‘.btn’).addEventListener(‘click’,()=>{ console.log(‘afterclick’,state); setState(state-1); }) },[state]) return ( <> <button className="btn">click me</button> </> ); } the output looks like this(no click of button): outside 2 the output looks like this(after one click of button): outside 2 afterclick 2 outside 1 everything works as expected… Read More I have a piece of code that does not work as expected, need an explanation

Why does addEventListener is sending logs 6 times whenever I click a hyperlink in a page?

my addEventListener is sending logs 6 times whenever I click a hyperlink in a page. I have been checking what causing it and I couldn’t figure out why. I know there are similar questions like this in this forum but I don’t know why it’s sending 6 logs to my webhook url. Appreciate all the… Read More Why does addEventListener is sending logs 6 times whenever I click a hyperlink in a page?