@@ -12,46 +12,64 @@ wordInput.addEventListener("keydown", (event) => {
1212} ) ;
1313
1414searchBtn . 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} ) ;
5563const 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+ } ;
0 commit comments