-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
81 lines (64 loc) · 2.46 KB
/
Copy pathscript.js
File metadata and controls
81 lines (64 loc) · 2.46 KB
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
document.addEventListener('DOMContentLoaded', () => {
// Gerekli HTML elementlerini seçme
const kelimeInput = document.getElementById('kelimeInput');
const anlamInput = document.getElementById('anlamInput');
const ekleBtn = document.getElementById('ekleBtn');
const kelimeListesi = document.getElementById('kelimeListesi');
// Uygulama yüklendiğinde Local Storage'dan kelimeleri al ve listeye ekle
loadWords();
// Ekle butonuna tıklanınca kelime ekleme fonksiyonunu çağır
ekleBtn.addEventListener('click', addWord);
// Enter tuşuyla da kelime ekleyebilmek için
kelimeInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
addWord();
}
});
anlamInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
addWord();
}
});
// Kelime ekleme fonksiyonu
function addWord() {
const kelime = kelimeInput.value.trim();
const anlam = anlamInput.value.trim();
if (kelime === '' || anlam === '') {
alert('Lütfen hem kelimeyi hem de anlamını girin.');
return;
}
const newWord = { kelime: kelime, anlam: anlam };
// Kelimeleri Local Storage'a kaydet
saveWord(newWord);
// Listeye yeni kelimeyi ekle
displayWord(newWord);
// Giriş alanlarını temizle
kelimeInput.value = '';
anlamInput.value = '';
kelimeInput.focus();
}
// Kelimeyi Local Storage'a kaydeden fonksiyon
function saveWord(word) {
// Mevcut kelimeleri al, yoksa boş bir array oluştur
let words = JSON.parse(localStorage.getItem('words')) || [];
// Yeni kelimeyi ekle
words.push(word);
// Güncel listeyi Local Storage'a kaydet
localStorage.setItem('words', JSON.stringify(words));
}
// Local Storage'dan kelimeleri alıp ekrana yazdıran fonksiyon
function loadWords() {
let words = JSON.parse(localStorage.getItem('words')) || [];
words.forEach(displayWord);
}
// Kelimeyi HTML listesine ekleyen fonksiyon
function displayWord(word) {
const listItem = document.createElement('li');
// Kelime ve anlamı gösterecek HTML yapısı
listItem.innerHTML = `
<span>${word.kelime}</span>
<span class="anlam">(${word.anlam})</span>
`;
kelimeListesi.appendChild(listItem);
}
});