File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ const noteTitle = document.getElementById("noteTitle");
2+ const noteContent = document.getElementById("noteContent");
3+ const addNoteBtn = document.getElementById("addNote");
4+ const notesList = document.getElementById("notesList");
5+
6+ // Add note
7+ addNoteBtn.addEventListener("click", () => {
8+ const title = noteTitle.value.trim();
9+ const content = noteContent.value.trim();
10+ if (!title || !content) return alert("Both fields required!");
11+
12+ db.collection("notes").add({ title, content, timestamp: Date.now() })
13+ .then(() => {
14+ noteTitle.value = "";
15+ noteContent.value = "";
16+ });
17+ });
18+
19+ // Real-time listener
20+ db.collection("notes").orderBy("timestamp", "desc")
21+ .onSnapshot(snapshot => {
22+ notesList.innerHTML = "";
23+ snapshot.forEach(doc => {
24+ const note = doc.data();
25+ const div = document.createElement("div");
26+ div.className = "note-card";
27+ div.innerHTML = `
28+ <h3>${note.title}</h3>
29+ <p>${note.content}</p>
30+ <button onclick="deleteNote('${doc.id}')">Delete</button>
31+ `;
32+ notesList.appendChild(div);
33+ });
34+ });
35+
36+ // Delete note
37+ function deleteNote(id) {
38+ db.collection("notes").doc(id).delete();
39+ }
You can’t perform that action at this time.
0 commit comments