Skip to content

Commit 0d31d10

Browse files
dictionary in progress
1 parent 0a1cf1b commit 0d31d10

File tree

3 files changed

+62
-27
lines changed

3 files changed

+62
-27
lines changed

Chapter10/dictionaryUsingAPI/divineDefine.js

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,64 @@ wordInput.addEventListener("keydown", (event) => {
1212
});
1313

1414
searchBtn.addEventListener("click", () => {
15-
let wordInput = document.getElementById("wordInput").value;
15+
const wordInput = document.getElementById("wordInput").value;
1616
console.log(wordInput);
17-
18-
fetch(`${url}${wordInput}`)
19-
.then((response) => {
20-
return response.json();
21-
})
22-
.then((data) => {
23-
result.innerHTML = ` <div id="word">
17+
if (!wordInput == "") {
18+
fetch(`${url}${wordInput}`)
19+
.then((response) => {
20+
return response.json();
21+
})
22+
.then((data) => {
23+
result.innerHTML = ` <div id="word">
2424
<h3>${wordInput}</h3><button onclick="pronounce()"><i class="fa-solid fa-volume-high"></i></button>
2525
</div>
2626
<div id="details">
2727
<p>${data[0].meanings[0].partOfSpeech}</p>
2828
<p>/${data[0].phonetic}/</p>
2929
</div>
30-
<p id="wordMeaning">${
31-
data[0].meanings[0].definitions[0].definition
32-
}</p>
30+
<div id="wordMeaning-copyBtn">
31+
<p id="wordMeaning">${
32+
data[0].meanings[0].definitions[0].definition
33+
}</p>
34+
<button id="copyBtn"><i class="fa-solid fa-copy"></i></button>
35+
</div>
3336
<p id="wordExample">${
3437
data[0].meanings[0].definitions[0].example ||
3538
data[0].meanings[1]?.definitions[0]?.example ||
3639
"null"
3740
}</p>`;
38-
sound.setAttribute(
39-
"src",
40-
`${
41-
data[0].phonetics[0]?.audio || //Optional Chaining (?.) Operator chatGPT's suggestion
42-
data[0].phonetics[1]?.audio ||
43-
data[0].phonetics[2]?.audio
44-
}`
45-
);
46-
console.log(data);
47-
console.log(sound);
48-
})
49-
.catch((error) => {
50-
result.innerHTML = `<h3 id="errorMessage">Sorry pal, we couldn't find definitions for the word you were looking for.</h3>`;
51-
console.log(error);
52-
console.log(error.message);
53-
});
41+
sound.setAttribute(
42+
"src",
43+
`${
44+
data[0].phonetics[0]?.audio || //Optional Chaining (?.) Operator chatGPT's suggestion
45+
data[0].phonetics[1]?.audio ||
46+
data[0].phonetics[2]?.audio
47+
}`
48+
);
49+
const copyBtn = document.getElementById("copyBtn");
50+
copyBtn.addEventListener("click", () => {
51+
copyToClipboard(data[0].meanings[0].definitions[0].definition);
52+
});
53+
})
54+
.catch((error) => {
55+
result.innerHTML = `<h3 id="errorMessage">Sorry pal, we couldn't find definitions for the word you were looking for.</h3>`;
56+
console.log(error);
57+
console.log(error.message);
58+
});
59+
} else {
60+
result.innerHTML = `<h3 id="errorMessage">Atleast provide me a word man!</h3>`;
61+
}
5462
});
5563
const pronounce = () => {
5664
sound.play();
5765
};
66+
const copyToClipboard = (text) => {
67+
navigator.clipboard
68+
.writeText(text)
69+
.then(() => {
70+
console.log(`${text} is copied to clipboard!`);
71+
})
72+
.catch((error) => {
73+
console.error("Copy failed:", error);
74+
});
75+
};

Chapter10/dictionaryUsingAPI/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<input type="text" placeholder="Type the word here..." id="wordInput"><button id="searchBtn">Search</button>
2828
</div>
2929
<div id="result">
30+
3031
</div>
3132
</div>
3233

Chapter10/dictionaryUsingAPI/style.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,19 @@ body {
9696
font-size: 2vmin;
9797
color: #575a7b;
9898
}
99+
#wordMeaning-copyBtn {
100+
width: 100%;
101+
display: flex;
102+
justify-content: space-between;
103+
}
104+
#copyBtn {
105+
padding: 1vmin;
106+
background-color: transparent;
107+
color: #f4d03f;
108+
border: none;
109+
outline: none;
110+
font-size: 1.7vmax;
111+
}
99112

100113
@media (max-width: 575.98px) {
101114
#container {
@@ -142,6 +155,9 @@ body {
142155
margin-top: 3vmax;
143156
font-size: 2vmin;
144157
}
158+
#copyBtn {
159+
font-size: 3.6vmax;
160+
}
145161
}
146162
@media (min-width: 576px) and (max-width: 767.98px) {
147163
}

0 commit comments

Comments
 (0)