-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
40 lines (31 loc) · 950 Bytes
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var button = document.getElementById("enter");
var input = document.getElementById("userinput");
var ul = document.querySelector("ul");
button.addEventListener("click", addEventAfterClick);
input.addEventListener("keypress", addEventAfterKey);
function inputLength() {
return input.value.length;
}
function addListElement() {
var li = document.createElement("li");
li.appendChild(document.createTextNode(input.value));
ul.appendChild(li);
input.value = "";
}
function addEventAfterClick() {
if (inputLength() > 0) {
addListElement();
}
}
function addEventAfterKey(event) {
if (inputLength() > 0 && event.keyCode === 13) {
addListElement();
}
}
// Also alternative version is:
// source: <div id="one">one</div>
// var d1 = document.getElementById('one');
// d1.insertAdjacentHTML('afterend', '<div id="two">two</div>');
// At this point, the new structure is:
// <div id="one">one</div
// <div id="two">two</div>