-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
70 lines (62 loc) · 1.86 KB
/
main.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// your code here
const tweetForm = document.querySelector('form');
const tweetLi = document.createElement('li');
const randomTweet = document.querySelector('#btn__new-tweet');
const viewAll = document.querySelector('#btn__view-all');
tweetForm.addEventListener('submit', function(e) {
e.preventDefault();
let inputValue = newTweet();
let date = new Date();
let tweet = {...inputValue, created_at : date.format()};
console.log(tweet);
if (tweet.user === '') {
alert('이름을 입력해주세요!')
} else if (tweet.comment === '') {
alert('내용을 입력해주세요!')
} else {
makeDom(tweet);
addData(tweet);
}
})
function renderTweet() {
for(let i = 0; i < DATA.length; i++) {
makeDom(DATA[i]);
}
}
renderTweet();
randomTweet.addEventListener('click', function() {
//메소드를 사용해서 3개 랜덤 생성
let randomTweet = [];
for (let i = 0; i < 3; i++) {
randomTweet.push(generateNewTweet())
}
//생성한 객체들을 데이터에 추가
for (let i = 0; i < randomTweet.length; i++) {
addData(randomTweet[i]);
}
//생성한 객체들을 HTML에 추가
for (let i = 0; i < randomTweet.length; i++) {
makeDom(randomTweet[i]);
}
})
ul.addEventListener('click', function(e){
if (e.target.id === 'twitt__name') {
let tempArr = [];
for (let i = 0; i < DATA.length; i++) {
if (e.target.textContent === DATA[i].user) {
tempArr.push(DATA[i]);
}
}
removeTweet();
for (let i = 0; i < tempArr.length; i++) {
makeDom(tempArr[i]);
}
}
})
viewAll.addEventListener('click', function () {
removeTweet();
for(let i = 0; i < DATA.length; i++) {
makeDom(DATA[i]);
}
})
//event bubbling event capturing 나중에 검색