1

I tried to make it so that a string gets logged in the console every time I click on a button. However, nothing is logged. Why so?

<!DOCTYPE html>
<html>
<head>
  <script type="text/javascript">
  function log_a_string_plz() {
    console.log("some string i want logged");
  }
  document.onload = function() {
    document.getElementById("my_wonderful_button").onclick = log_a_string_plz;
  }
  </script>
  <title>logging a string test</title>
</head>
<body>
  <button id="my_wonderful_button">log a string!</button>
</body>
</html>

I've tried changing event handlers to no avail.

dangerChihuahua007
  • 20,299
  • 35
  • 117
  • 206

1 Answers1

1

Try:

window.onload = function () { 
    document.getElementById("my_wonderful_button").onclick = log_a_string_plz;
}

jsFiddle example

j08691
  • 204,283
  • 31
  • 260
  • 272