Skip to content

Commit e104e98

Browse files
committed
adding change user function
1 parent 27e98c2 commit e104e98

4 files changed

Lines changed: 41 additions & 4 deletions

File tree

images/firefox2.png

37.6 KB
Loading
127 Bytes
Binary file not shown.

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@ <h1> Mozilla Firefox is Fire! </h1>
4646
<a href="https://www.mozilla.org/en-US/about/manifesto/"> Mozilla Manifesto</a>
4747
outlines the values and principles that guide our work.
4848
</p>
49+
<button>Change user</button>
4950
</body>
5051
</html>

scripts/main.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,40 @@
1-
// Store a reference to the <h1> in a variable
2-
const myHeading = document.querySelector("h1");
3-
// Update the text content of the <h1>
4-
myHeading.textContent = "Hello world!";
1+
const myImage = document.querySelector("img");
2+
3+
myImage.addEventListener("click", () => {
4+
const mySrc = myImage.getAttribute("src");
5+
if (mySrc === "images/firefox-icon.png") {
6+
myImage.setAttribute("src", "images/firefox2.png");
7+
} else {
8+
myImage.setAttribute("src", "images/firefox-icon.png");
9+
}
10+
});
11+
12+
let myButton = document.querySelector("button");
13+
let myHeading = document.querySelector("h1");
14+
15+
function setUserName() {
16+
const myName = prompt("Please enter your name.");
17+
localStorage.setItem("name", myName);
18+
myHeading.textContent = `Mozilla is cool, ${myName}`;
19+
}
20+
21+
if (!localStorage.getItem("name")) {
22+
setUserName();
23+
} else {
24+
const storedName = localStorage.getItem("name");
25+
myHeading.textContent = `Mozilla is cool, ${storedName}`;
26+
}
27+
28+
myButton.addEventListener("click", () => {
29+
setUserName();
30+
});
31+
32+
function setUserName() {
33+
const myName = prompt("Please enter your name.");
34+
if (!myName) {
35+
setUserName();
36+
} else {
37+
localStorage.setItem("name", myName);
38+
myHeading.textContent = `Mozilla is cool, ${myName}`;
39+
}
40+
}

0 commit comments

Comments
 (0)