Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function attachEvents() {
- let authorInp = document.getElementById("author");
- let contentInp = document.getElementById("content");
- let sendBtn = document.getElementById("submit");
- let refreshBtn = document.getElementById("refresh");
- let chatBox = document.getElementById("messages");
- console.log(chatBox)
- sendBtn.addEventListener("click", function () {
- let authorName = authorInp.value;
- let msgText = contentInp.value;
- let msgObj = {
- author: authorName,
- content: msgText
- }
- console.log(msgObj)
- fetch('https://rest-messanger.firebaseio.com/messanger.json', {
- method: 'post',
- headers: {
- 'Content-type': 'application/json'
- },
- body: JSON.stringify(msgObj),
- })
- contentInp.value="";
- })
- refreshBtn.addEventListener("click", function () {
- fetch('https://rest-messanger.firebaseio.com/messanger.json')
- .then(res => res.json())
- .then(data => {
- let arrayResponse = Object.entries(data);
- arrayResponse.forEach(obj => {
- console.log(obj)
- let composedMessage = `${obj[1].author}: ${obj[1].content}`
- chatBox.textContent += `${composedMessage}\n`;
- authorInp.value="";
- contentInp.value="";
- });
- })
- })
- }
- attachEvents();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement