1

I began to study the "javascript".

in html source code

<!Doctype html>
<html>
<head>
    <meta charset="utf-8">
</head>
<body>
    <button id="test_button">Test</button>
    <p id="test">Hello World</p>

    <!-- script -->
    <script src="test.js"></script>
</body>
</html>

in javascript source code

"use strict";

$(function() {
document.getElementById("test_button").onclick = test_click;
function test_click() {
    document.getElementById("test").innerHTML = "HI";
}
});

Where's wrong?

j08691
  • 190,436
  • 28
  • 232
  • 252
안상욱
  • 45
  • 1
  • 1
  • 5

2 Answers2

4

You need to include jQuery. Try adding this to your <head>:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
Elliot B.
  • 14,589
  • 9
  • 70
  • 99
1

$ is an alias for the jQuery library. In this case, you don't need jQuery at all. You can just execute that IIFE (immediately-invoked function expression) and it should work fine.

"use strict";

(function() {
document.getElementById("test_button").onclick = test_click;
function test_click() {
    document.getElementById("test").innerHTML = "HI";
}
})();
<!Doctype html>
<html>
<head>
    <meta charset="utf-8">
</head>
<body>
    <button id="test_button">Test</button>
    <p id="test">Hello World</p>

    <!-- script -->
    <script src="test.js"></script>
</body>
</html>
10100111001
  • 1,712
  • 1
  • 9
  • 7