let strangeButtonCmp = document.getElementById("strangeButton");
function clickHandler(event) {
console.log('Hey, you just pressed strange button');
alert('Heloooo');
}
strangeButtonCmp.addEventListener("click", clickHandler);
let nameOfVariable;
nameOfVariable = 5;
(you can put in any kind of value)
nameOfVariable
(write the variable name wherever you would write a value)
let nameOfVariable;
nameOfVariable = 5;
... is the same as
let nameOfVariable = 5;
let name;
name = prompt("What's your name?");
alert("Hi " + name + "!");
Shorter:
let name = prompt("What's your name?");
alert("Hi " + name + "!");
xxx
a good variable name?myVariable
a good variable name?age
a good variable name?alert("Hello world!");
prompt("What is your name?");
function nameOfFunction() {
// body (your code)
// ...
// ...
}
(nameOfFunction is choosen by YOU)
nameOfFunction();
(can be used in any line in the code after the function was defined)
let weather;
weather = prompt("How's the weather right now?");
alert("Oh, so it's " + weather + ", eh?");
let weather1;
weather1 = prompt("How's the weather right now?");
alert("Oh, so it's " + weather1 + ", eh?");
...
let weather2;
weather2 = prompt("How's the weather right now?");
alert("Oh, so it's " + weather2 + ", eh?");
...
let weather3;
weather3 = prompt("How's the weather right now?");
alert("Oh, so it's " + weather3 + ", eh?");
Do we have to write that every time?
function askForWeather() {
let weather;
weather = prompt("How's the weather right now?");
alert("Oh, so it's " + weather + ", eh?");
}
askForWeather();
...
askForWeather();
...
askForWeather();
How are you?
let goodButtonCmp = document.getElementById("goodButton");
let badButtonCmp = document.getElementById("badButton");
let answerCmp = document.getElementById("answer");
function goodButtonClick(event) {
answerCmp.textContent = "That's great 😊";
}
function badButtonClick(event) {
answerCmp.textContent = "Oh that's sad 😥";
}
goodButtonCmp.addEventListener("click", goodButtonClick);
badButtonCmp.addEventListener("click", badButtonClick);
let goodButtonCmp = document.getElementById("goodButton");
function goodButtonClick(event) {
// do something here
}
goodButtonCmp.addEventListener("click", goodButtonClick);
We will use <script/> tag inside index.html
<html>
<head>
<meta charset="utf-8">
<title>My HTML Page</title>
</head>
<body>
...
<script type="text/javascript">
...
</script>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>My HTML Page</title>
</head>
<body>
<p>How are you?</p>
<button id="goodButton">Good 👍</button><button id="badButton">Bad 👎</button>
<p id="answer"></p>
<script>
let goodButtonCmp = document.getElementById("goodButton");
let badButtonCmp = document.getElementById("badButton");
let answerCmp = document.getElementById("answer");
function goodButtonClick(event) {
answerCmp.textContent = "That's great 😊";
}
function badButtonClick(event) {
answerCmp.textContent = "Oh that's sad 😥";
}
goodButtonCmp.addEventListener("click", goodButtonClick);
badButtonCmp.addEventListener("click", badButtonClick);
</script>
</body>
</html>