-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
127 lines (105 loc) · 3.15 KB
/
app.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//variables
let tweetList = document.getElementById('tweet-list'),
noOfItem = document.querySelector('.fixedln p')
noOfItem.textContent=getTweetLocalStorage().length
function backgroundC(){
if(noOfItem.textContent==0){
noOfItem.style.background='red';
}else{
noOfItem.style.background='';
}
}
backgroundC()
//Listener
eventListener()
function eventListener(){
//Submit new tweet
document.getElementById('form').addEventListener('submit', newTweet);
//Remove listner
tweetList.addEventListener('click', removeTweet);
//document
document.addEventListener('DOMContentLoaded', localStorageTweets)
}
//function
function newTweet(){
let tweet = document.getElementById('tweetmsg').value;
//create an <li> element
let li = document.createElement('li');
li.textContent = tweet;
noOfItem.textContent=getTweetLocalStorage().length+1
backgroundC()
//Create Remove Button
let removeBtn = document.createElement('a');
removeBtn.classList='remove-tweet';
removeBtn.href='#';
removeBtn.textContent="X";
li.appendChild(removeBtn);
if(tweet === ''){
let textstyle = document.getElementById('tweetmsg');
textstyle.style.borderColor="red";
return
}else{
let textstyle = document.getElementById('tweetmsg');
textstyle.style.borderColor="#999";
}
//append li
tweetList.appendChild(li);
//setTweetLocalStorage
setTweetLocalStorage(tweet);
this.reset();
}
function removeTweet(e){
if(e.target.classList.contains('remove-tweet')){
e.target.parentElement.remove()
noOfItem.textContent=getTweetLocalStorage().length-1
backgroundC()
}
//removeLocalStorage
removeLocalStorage(e.target.parentElement.textContent);
}
//setTweetLocalStorage
function setTweetLocalStorage(tweet){
let tweets = getTweetLocalStorage();
tweets.push(tweet)
//convert value into string
localStorage.setItem('tweets', JSON.stringify( tweets ))
}
//getTweetLocalStorage
function getTweetLocalStorage(){
let tweets;
if(localStorage.getItem('tweets')===null){
tweets =[];
}else{
tweets = JSON.parse(localStorage.getItem('tweets'))
}
return tweets;
}
//localStorageTweets
function localStorageTweets(){
let tweets = getTweetLocalStorage();
tweets.forEach(function(tweet){
//create an <li> element
let li = document.createElement('li');
li.textContent = tweet;
//Create Remove Button
let removeBtn = document.createElement('a');
removeBtn.classList='remove-tweet';
removeBtn.href='#';
removeBtn.textContent="X";
li.appendChild(removeBtn);
//append li
tweetList.appendChild(li);
})
}
//removeLocalStorage
function removeLocalStorage(tweet){
let tweets = getTweetLocalStorage();
//remove last charachter i.e = 'X'
let deleteTweet = tweet.substring(0, tweet.length-1);
tweets.forEach(function(tweetLS, index){
if(deleteTweet === tweetLS){
tweets.splice(index,1)
}
localStorage.setItem('tweets', JSON.stringify( tweets ))
})
}