[Solved]Event triggered at "appendChild" and not when clicking

[Solved]Event triggered at "appendChild" and not when clicking
0.0 0

#1

Hello everyone, I am trying to make an “app” that reminds the user to meditate random intervals.

Right now, I am setting up the buttons that specify if the user is ready to do the task or not.

When the page loads the event that is supposed to be triggered by clicking the yes button is triggered automatically and cannot be triggered by clicking the button at all. Any help would be greatly appreciated!
:slight_smile:

(To be more specific about the app, there will be a loop that will exit at a random moment and the app will present the user with two options. If the user clicks yes a timer will be triggered, during which the task should be performed. If the user clicks no, the app will revert back to the loop, until it alerts the user once again.)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Random Reminder</title>
</head>
<body>
    <div id="container">
        <h1>Time to Meditate!</h1>
                
                
                
        <button id="debug" onclick="test()">Debug</button>
     </div>




<script src="main.js"></script>
</body>
</html>
  var yesBtn = document.createElement("button");
  var noBtn = document.createElement("button");
  
  yesBtn.innerHTML = "Yes";
  noBtn.innerHTML = "No";

  yesBtn.addEventListener("click", console.log(1));
  yesNo();
  
  function yesNo(){
    var cont = document.getElementById('container');
    cont.appendChild(yesBtn).setAttribute("id", "yes");
    cont.appendChild(noBtn).setAttribute("id", "no");
  }
  
//the debug button is for testing purposes, it does nothing right now

#2

mebbe try…

yesBtn.addEventListener("click",dbug );
  function dbug(){console.log(1);}

#3

Your suggestion did not seem to work at first but I edited your solutiom a bit and it worked. Thanks a lot for the input! I was really stuck! :smiley: :smiley:

yesBtn.addEventListener('click', function(){console.log(1)});