×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Peter Hempel
Added: Sep 17, 2017 2:45 PM
Views: 5
add a function to a button with an Event Listener
  1. <p>This example uses the addEventListener() method to attach a click event to a button.</p>
  2. <button id="myBtn">Try it</button>
  3. <p id="demo"></p>
  4. <script>
  5. document.getElementById("myBtn").addEventListener("click", displayDate);
  6. function displayDate() {
  7.     document.getElementById("demo").innerHTML = Date();
  8. }
  9. </script>