A
A
Artem Vladilenko2020-07-11 22:57:05
JavaScript
Artem Vladilenko, 2020-07-11 22:57:05

How to make the function work only when a person presses all 3 buttons?

let's say I have a certain code and I need this code:
$(".year, .mouth, .day").click(function (event) {
console.log( new Date(event.targer));
});
called only when the person presses all the buttons. How to do it without jQuery?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
N
NAHB, 2020-07-12
@kurskiev_official

<button class="year" name="date">
Year
</button>
<button class="mouth" name="date">
Mouth
</button>
<button class="day" name="date">
Day
</button>

let buttons = document.getElementsByName("date");
for(let btn of buttons) {

    btn.addEventListener("click", function(e) {
        e.target.dataset.ready = true;
  
        let isReady = true;
        for(let btn of buttons) {
            if(!btn.dataset.ready) {
               isReady = false;
            }
        }
        
        if(isReady) {
    	    console.log("Сюда можете вставить ваш код");
        }
  });
  
}

S
Stalkerdp, 2020-07-11
@Stalkerdp

You can create an empty string variable and, by clicking on the first two buttons, add letters to the string, for example
let checkPres="";
checkPres +="a";
checkPres +="b";
And on button 3
if (checkPres=="ab"){}

F
FlatUser, 2020-07-12
@FlatUser

Can it be like this

let clickCount = 0;
$(".year, .mouth, .day").click(function (event) {
  if (clickCount === 3) {
      console.log( new Date(event.targer));
  }
});

R
Rerurk, 2020-07-12
@Rerurk

<body>
<button name="bt" value="1">1</button>
<button name="bt" value="2">2</button>
<button name="bt" value="3">3</button>
<script>
  let sum=0;
  let s=document.getElementsByName('bt');
    s.forEach(item=>item.onmousedown=()=>btclick(item))

  let btclick=(bt)=>{
        sum=sum+parseInt(bt.value)
    console.log(bt.value+' pressedDown'+' sum='+sum);
    bt.style.background='green'
      bt.onmousedown=()=>btUnClick(bt)
    testAllBtIsPress(sum)
    }
  let btUnClick=(bt)=>{
    	bt.style.background='grey';
    	bt.onmousedown=()=>btclick(bt);
    sum=sum-parseInt(bt.value)
    console.log(bt.value+' pressedUP'+'sum='+sum);

  }

  let testAllBtIsPress=(a)=>{
    	if (a===6){alert('ТАДАМ!! AllButtonsPressed')}
  }

</script>
</body>

S
Stalker_RED, 2020-07-12
@Stalker_RED

With these buttons, the user sets the date? Then check for clicks on the buttons, and the valid date at the exit. And from him he invites you on February 31, for example.
Even better, replace with input type="date"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question