Coin Flipper

Javascript if not clicking on this element js

To check for descendant elements, use the universal selector . Demo Following snippet shows an example:Alternative Solution Instead of using the method, use the combination of and methods to check if the is the element or is a child element of the element.

Javascript click event handler fires without clicking

You are directly calling it.

document.getElementById("main_btn").addEventListener("click", hideId("main"); 

You should do that in a callback.

document.getElementById("main_btn").addEventListener("click", function ()< hideId("main"); >); 

This code executes your function hideId(«main») you should pass just the callback’s name:

document.getElementById("main_btn").addEventListener("click", hideId); function hideId(event) < var // get the id of the clicked element document.getElementById(data).style.display = "none"; console.log("hidden element #"+data); >
document.getElementById("main_btn").addEventListener("click", hideId.bind(null, "main"); 

Click on document but not on a specific div with pure JavaScript, like .addEventListener(«click», () => < return false; >); ? – Taki. Jun 29, 2018 at 16:54 · yes but when clicked on document should return true.

Читайте также:  Максимальное целое число javascript

Click event not working on inner tag elements

Your code only checks if the event.target is the .collapse-btn element or not. You also need to check if the clicked element is a descendant element of the .collapse-btn element.

To achieve the desired result, you can use the two conditions to check if the event.target is the .collapse-btn or any of its descendant. To check for descendant elements, use the universal selector * .

parent.addEventListener('click', (e) => < if ( e.target.matches('.collapse-btn') || e.target.matches('.collapse-btn *') ) < // code >>); 
Demo

Following snippet shows an example:

const parent = document.querySelector('.container'); parent.addEventListener('click', e => < if ( e.target.matches('.collapse-btn') || e.target.matches('.collapse-btn *') ) < parent.classList.toggle('active'); >>);
.container < background: #eee; width: 120px; height: 120px; >.container.active < border: 2px solid; >.collapse-btn < background: #999; padding: 10px; >.collapse-btn span

You could also put the selectors in an array and then use .some() method to check if event.target matches ay of the selector in the array.

const selectors = ['.collapse-btn', '.collapse-btn *']; parent.addEventListener('click', e => < if (selectors.some(s =>e.target.matches(s))) < // code >>); 
Demo

Following snippet shows an example:

const parent = document.querySelector('.container'); parent.addEventListener('click', e => < const selectors = ['.collapse-btn', '.collapse-btn *']; if (selectors.some(s =>e.target.matches(s))) < parent.classList.toggle('active'); >>);
.container < background: #eee; width: 120px; height: 120px; >.container.active < border: 2px solid; >.collapse-btn < background: #999; padding: 10px; >.collapse-btn span
Alternative Solution

Instead of using the .matches() method, use the combination of Element.closest() and Node.contains() methods to check if the event.target is the .collapse-btn element or is a child element of the .collapse-btn element.

For this to work, you need to do two steps:

  1. You first need to get the .collapse-btn that is the closest ancestor of the event.target .
  2. After that, you need to check if the event.target is a descendant of the button selected in step 1.
parent.addEventListener('click', (e) => < const targetCollapseBtn = e.target.closest('.collapse-btn'); if (targetCollapseBtn && targetCollapseBtn.contains(e.target)) < . >>); 

If event.target is the .collapse-btn itself, then

e.target.closest('.collpase-btn') 

will return the e.target , i.e. .collapse-btn and

targetCollapseBtn.contains(e.target) 

will also return true because .contains() method returns true even if the node passed to it as an argument is the same node as the one on which .contains() method was called.

So using .closest() and .contains() methods in this way covers both the use cases, i.e. when .collapse-btn is clicked or when any of its descendant element is clicked.

Demo

Following snippet shows a simple example:

const parent = document.querySelector('.container'); parent.addEventListener('click', e => < const targetCollapseBtn = e.target.closest('.collapse-btn'); if (targetCollapseBtn && targetCollapseBtn.contains(e.target)) < parent.classList.toggle('active'); >>);
.container < background: #eee; width: 120px; height: 120px; >.container.active < border: 2px solid; >.collapse-btn < background: #999; padding: 10px; >.collapse-btn span

Click() method not working and Console returns an error message in, In this article, we will see “Uncaught TypeError: document.getElementsByClassName(…).click is not a function” error created while using click()

How to hide an element if a button was not clicked for a certain amount of time

Use setTimeout() and clearTimeout() :

  • const hide = setTimeout() in main scope, with your delay as a second argument
  • clearTimeout(hide) on the button click
  • element.style.display = «none» to hide the element
  • optionally, as you use Bootstrap, I guess you can use jQuery .hide() instead
let heads = 0, tails = 0;let coinFlip = () => < clearTimeout(hide); let flip = () => < return Math.floor((Math.random() * 2) + 1); >let result = flip(); if (result === 1) < heads++; document.getElementById("winMessage").innerHTML = "Heads!" >else if (result === 2) < tails++; document.getElementById("winMessage").innerHTML = "Tails!" >document.getElementById("heads").innerText = heads; document.getElementById("tails").innerHTML = tails; >const hide = setTimeout(() => < document.getElementById('winMessage').style.display = 'none' >, 3000)
  

Coin Flip

Hides after 3000ms
Heads Count:
Tails Count:
Solution 2:

For the simple way use css. With css animation.

@keyframes showHide < 0% < opacity: 0; >10% < opacity: 0.2 >20% < opacity: 0.35 >50% < opacity: 1 >60% < opacity: 0.75 >100 % < opacity: 0; >> #winMessage

You should start a timer at the start of your program. Every time the button is pressed then the timer should be erased and a new timer should start. Here is an example using setTimeout and clearTimeout.

var time = 5000; var timeoutID = setTimeout(hideElement, time);document.querySelector("#button").addEventListener("click", delay);function delay() < clearTimeout(timeoutID); timeoutID = setTimeout(hideElement, time); >function hideElement()
 This is how it would look like on your question:
let heads = 0, tails = 0;const time = 5000; let timeoutID = setTimeout(hideMessage, time);let coinFlip = () => < let flip = () => < return Math.floor((Math.random() * 2) + 1); >let result = flip(); if (result === 1) < heads++; document.getElementById("winMessage").innerHTML = "Heads!" >else if (result === 2) < tails++; document.getElementById("winMessage").innerHTML = "Tails!" >document.getElementById("heads").innerText = heads; document.getElementById("tails").innerHTML = tails; clearTimeout(timeoutID); timeoutID = setTimeout(hideMessage, time); // If you do not want it to reappear, remove this line document.querySelector("#winMessage").style.display = null; >;function hideMessage()
    

Coin Flip

Heads Count:
Tails Count:

How to click a specific DOM element (not button) with pure js that act, So the task is just click on that element, and after that internal site js would process this click and give me popup. Awaited event is like

Источник

how to check if a button is clicked in javascript

Hello Everyone, today I’m going to share how do you check a button is clicked or not in JavaScript. Suppose you have added a search icon on your website and when a user will click on the search icon, the search box will appear. So this is very important and you must know about that.

There are many ways to check if a button is clicked or not in JavaScript . I shall share some ways.

Answer 1 :- Using Click Event

In the first example, I shall use the click event to check whether a button is clicked. First of all create a button with an id attribute. Then write javascript code between the tag.

 var seletBtn= document.getElementById('demoBtn'); selectBtn.onclick = function() 

Answer 2 :- Using addEventListener

You can use addEventListener instead of click event.

Demo Using addEventListener

  1. id Attribute : document.querySelector(«#demoBtn»)
  2. class Attribute :document.querySelector(«.demoBtn»)
  3. tag name :document.querySelector(«button»)

Answer 3 :- Using onclick event

Answer 4 :- How many times a button is clicked?

First, create a html button with an id as id=»demoBtn» .

Then add the js code between the tag. First, store the button in a variable using js , create a variable and initialize it to 0.

How many times a button is clicked

In this post, I have explained how to check whether a Button is clicked using JavaScript. I hope you have learned something new in this tutorial.

Источник

If not clicked javascript

Last updated: Jan 12, 2023
Reading time · 2 min

banner

# Check if Element was Clicked using JavaScript

To check if an element was clicked, add a click event listener to the element.

The click event is dispatched every time the element is clicked.

Here is the HTML for the examples.

Copied!
DOCTYPE html> html lang="en"> head> title>bobbyhadz.comtitle> meta charset="UTF-8" /> head> body> button id="btn" style="width: 100px; height: 100px"> Click me button> script src="index.js"> script> body> html>

And here is the related JavaScript code.

Copied!
const button = document.getElementById('btn'); button.addEventListener('click', function handleClick() console.log('element clicked'); >);

We added a click event listener to the button element.

# Check if an element has already been clicked before

If you need to detect if an element has already been clicked before, declare a variable in the outer scope and keep track of its value.

Copied!
const button = document.getElementById('btn'); let elementClicked = false; button.addEventListener('click', function handleClick() console.log('element clicked'); if (elementClicked) console.log('button has already been clicked before'); > elementClicked = true; >);

We declared the elementClicked variable and initialized its value to false .

# Only allow for the button to be clicked a single time

You can use this approach to only run the handleClick function the first time you click the element.

Copied!
const button = document.getElementById('btn'); let elementClicked = false; button.addEventListener('click', function handleClick() if (elementClicked) return; > console.log('element clicked'); elementClicked = true; >);

Once the handleClick function gets invoked, the value of the elementClicked variable is set to true .

On the next invocation of the function, we check if the value of the variable is truthy and return straight away.

If you try the example above, you will only see the element clicked message logged once to your console.

Note that you can add a click event listener to any type of element, it doesn’t have to be a button.

# Periodically check if an element has been clicked

You can also use the setInterval method to periodically check if an element has been clicked.

Copied!
const button = document.getElementById('btn'); let elementClicked = false; button.addEventListener('click', function handleClick() console.log('element clicked'); elementClicked = true; >); setInterval(() => if (elementClicked) console.log('element was clicked'); > else console.log("element hasn't been clicked yet"); > >, 1500); // 👉️ invoke every 1500 milliseconds

The function we passed to the setInterval method runs every 1.5 seconds and checks if the element has been clicked or not.

However, note that it is more efficient to use specific events, like click that only get dispatched when the element gets clicked.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.

Источник

Оцените статью