forked from raneemr05/MyPizzaStore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
314 lines (240 loc) · 10.3 KB
/
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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<<<<<<< HEAD
console.log("JS is loaded");
//Changing the title of website - <title>
document.title = "My Pizza Store";
console.log(document.title);
//Accessing a HTML element using its ID
document.getElementById("banner-text");
let bannerText = document.getElementById("banner-text");
// console.log(bannerText);
// let welcomeMessage = document.getElementById("welcome-message");
// console.log(welcomeMessage);
//Changes the value of the element
//
// console.log(bannerText.innerText); is not important that much it helps to debug your action(code)
// bannerText.innerText = "Serving crunchy Pizzaaaasss 🍕";
// bannerText.innerHTML = "<i> Serving crunchy Pizzaaaasss 🍕</i>";
// bannerText.innerContent = "<i> Serving crunchy Pizzaaaasss 🍕</i>";
function changeText(){
if(bannerText.innerText === "Delicious Pizzas, Anytime!") {
bannerText.innerText = "Serving Hot Pizza";
bannerText.style.color = "red";
}
else{
bannerText.innerText = "No Pizza today";
bannerText.style.color = "blue";
bannerText.style.backgroundColor = "white";
}
}
changeText();
// let bannerText = document.getElementById("banner-text");
// if(bannerText === document.getElementById("banner-text")) {
// console.log(bannerText.innerText = "Serving Hot Pizza");
// }
// else{
// console.log(bannerText.innerText = "No Pizza today");
// }
//-----------------------Changing the greeting message after a UserName
let userName = "Valeria"; //we have to declare a variable with a name as a value
let welcomeMessage = document.getElementById("welcome-message"); // then we declare a variable after its id
// Conditional if the user is assigned or not
if(userName){
welcomeMessage.innerText = "Welcome, " + userName + "!";
// creating an Element using .createElement
let specialOffer = document.createElement('h1'); //() here we put a part of html where we want to create an element
// Setting the content for the h1 element
specialOffer.innerText = "A Special Offer only for you!!!";
console.log(specialOffer); // only for debugging
// Once the element is created, we append it to DOM or HTML page
let aboutSection = document.querySelector(".about");
// console.log(aboutSection);
aboutSection.insertAdjacentElement("afterend", specialOffer);
// Appending the newly created element to the body\
// Adding new elements dynamically
// document.createElement('') then you add it to your dom
}
else {
let button = document.createElement('BUTTON');
button.innerText = "Sign in!";
let buttonSection = document.querySelector('.about');
buttonSection.insertAdjacentElement("beforebegin", button);
button.style.color = "yellow";
button.style.backgroundColor = "black";
button.style.padding = "50px 20px";
}
// ----------- REMOVE an ELEMENT
// let specialitySection = document.querySelector('.speciality');
// specialitySection.remove();
// ------------------------------- DOM DAY 2
// getElementByClassName
let navLinks = document.getElementsByClassName("nav-link");
console.log(navLinks);
for (let index = 0; index < navLinks.length; index++) {
navLinks[index].style.color = "green";
navLinks[index].style.fontSize = "14px";
}
// ----- getElementsByTagName and color all the h2 to blue
let headings = document.getElementsByTagName("h2");
console.log(headings);
for (let index = 0; index < headings.length; index++) {
headings[index].style.color = "blue";
}
// ------ creating an image in DOM
let offerImage = document.createElement("img");
offerImage.setAttribute("src","https://images.unsplash.com/photo-1579751626657-72bc17010498?q=80&w=2069&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D");
console.log(offerImage.getAttribute("src"));
// offerImage.style.width = "300px";
// offerImage.style.display = "block";
// offerImage.style.margin = "0 auto";
// --------- 2nd Approach: Use CSS Class
// with this Approach we can also add Id in CSS (in the same way)
offerImage.classList.add("centered-image");
console.log("Class name is: " + offerImage.className);
// offerImage.setAttribute("alt","alternative Text");
// console.log(offerImage.getAttribute("alt"));
document.body.appendChild(offerImage);
// -------EVENTS -----------------
//1. Event Hnadling
// let viewButton = document.querySelectorAll('.cta-btn'); // bc we have 2 classes with this name, it will be applied to the first class in the order
// console.log(viewButton);
// for (let index = 0; index < viewButton.length; index++) {
// viewButton[index].onclick = () => {
// console.log("Here we go!");
=======
console.log("JS is loading successfully");
//Changing the title of website - <title>
document.title = "My Pizza Store";
console.log(document.title);
// //Accessing a HTML element using its ID
// let bannerText = document.getElementById("banner-text");
// console.log(bannerText);
// // Changing the value of the element
// // innerText, innerHTML or textContent
// // bannerText.innerText = "<i> Serving Hot Pizzasss 🍕 </i>";
// // bannerText.innerHTML = "<i> Serving Hot Pizzasss 🍕 </i>"
// // bannerText.textContent = "Serving Hot Pizzasss 🍕";
// function changeText(){
// // Conditional text modification using DOM and JS based on the HTML element text
// if (bannerText.innerText === "Delicious Pizzas, Anytim") {
// bannerText.innerText = "Serving Hot Pizzas";
// // Manipulating the style using DOM style property
// bannerText.style.color = "red";
// bannerText.style.backgroundColor = "lightPink";
// } else {
// bannerText.innerText = "No Pizza today";
// bannerText.style.color = "blue";
// bannerText.style.backgroundColor ="lightBlue";
// }
// }
// changeText();
// ------ Changing the greeting message based on the username -----
// Specify the username or leave it blank to simulate a guest
let userName = "Valeria";
let welcomeMessage = document.getElementById("welcome-message");
// // Conditional to check if userName is assigned or not
// if(userName){
// console.log(userName);
// welcomeMessage.innerText = "Welcome, " + userName + "!";
// //Creating an element using createElement method
// let specialOffer = document.createElement('h1'); // <h1> </h1>
// // Setting the content for the h2 element
// specialOffer.innerText = "A Special Offer only for you!!!"; // <h1> A Special Offer only for you!!! </h1>
// console.log(specialOffer); //testing if the element is created successfully
// // Once the element is created we append it to the DOM / HTML Page
// // Accessing the about element using the class property
// // console.log(document.getElementsByClassName("about"));
// let aboutSection = document.querySelector(".about");
// // console.log(aboutSection);
// aboutSection.insertAdjacentElement("afterend", specialOffer);
// // Appending the newly created element to the body
// // document.body.appendChild(specialOffer);
// }
// else{
// console.log(userName);
// welcomeMessage.innerText = "Welcome Guest!";
// // ------ Adding new element dynamically -----
// // document.createElement('') then you add it to your DOM (appendChild)
// let signUpButton = document.createElement('button');
// signUpButton.innerText ="SIGN UP";
// signUpButton.style.color = "red";
// signUpButton.style.fontSize = "1.5em";
// signUpButton.style.backgroundColor = "Orange";
// signUpButton.style.padding = "5px";
// let aboutSection = document.querySelector(".about");
// aboutSection.insertAdjacentElement("afterend", signUpButton);
// // --- Remove an element ----
// let specialitySection = document.querySelector('.speciality');
// specialitySection.remove();
// }
// ------- DOM - DAY 2 ------
// ---- getElementsByClassName ----
// let navLinks = document.getElementsByClassName('nav-link');
// console.log(navLinks);
// for (let index = 0; index < navLinks.length; index++) {
// navLinks[index].style.color = "green";
// navLinks[index].style.fontSize = "14px";
// }
// // ---- getElementsByTagName and color all the h2 headings to Blue ----
// // ---- Attributes ----
// let offerImage = document.createElement("img");
// offerImage.setAttribute("src","https://plus.unsplash.com/premium_photo-1679924471066-dd984e92f395?q=80&w=1964&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D");
// console.log(offerImage.getAttribute("src"));
// // 1st Approach: Use inline styling
// // offerImage.style.width = "300px";
// // offerImage.style.display = "block";
// // offerImage.style.margin = "0 auto";
// // 2nd Approach : Use CSS Class
// offerImage.classList.add("centered-image");
// console.log("Class name is: " + offerImage.className);
// // set its alternative text using alt
// offerImage.setAttribute("alt", "A delicious pizza for you!");
// console.log(offerImage.getAttribute('alt'));
// // Append it to the page
// document.body.appendChild(offerImage);
// ----- EVENTS -----
// 1. Event Handling
// let viewButton = document.querySelectorAll('.cta-btn');
// console.log(viewButton);
// // let ctaBtn = document.getElementsByClassName('cta-btn');
// for (let index = 0; index < viewButton.length; index++) {
// viewButton[index].onclick = () => {
// console.log("Here you goo....");
>>>>>>> a856061371304a2557ce6b3e70b692f82de44d6a
// }
// }
<<<<<<< HEAD
let viewButton = document.getElementById('view-btn');
console.log(viewButton);
// viewButton.onclick = () => {
// console.log("I am clicked!");
// }
// viewButton.onclick = () => {
// console.log("I am clicked again!");
// }
// viewButton.onclick = () => {
// console.log("I am clicked again and again!");
// }
// ------- EVENT Listeners ----------
viewButton.addEventListener("click", () => {
console.log("Thank you again!!")
})
viewButton.addEventListener("click", () => {
console.log("Thank you twice!!")
})
viewButton.addEventListener("click", () => {
console.log("Thank you 3 times!!")
})
=======
// ---- EVENT HANDLER ----
let viewButton = document.getElementById('view-btn');
console.log(viewButton);
viewButton.onclick = () =>{
console.log("I am clicked!");
}
viewButton.onclick = () =>{
console.log("I am clicked again!");
}
viewButton.onclick = () =>{
console.log("I am clicked again and again!");
}
>>>>>>> a856061371304a2557ce6b3e70b692f82de44d6a