Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function attachEvents() {
- let adress = 'https://rest-messanger.firebaseio.com/messanger.json';
- let refreshBtn = document.querySelector('#refresh');
- refreshBtn.addEventListener('click', () => {
- fetch(adress)
- .then(response => response.json())
- .then(data => {
- let reducedMsg = Object.values(data).reduce((a, x) => {
- a += `${x.author}: ${x.content}\n`;
- }, '')
- let msgElem = document.querySelector('#messages')
- msgElem.textContent = reducedMsg;
- })
- })
- let enterBtn = document.querySelector('#submit');
- enterBtn.addEventListener('click', () => {
- let author = document.querySelector('#author');
- let content = document.querySelector('#content');
- fetch(adress, {
- method: 'POST',
- body: JSON.stringify({
- author: author.value,
- content: content.value,
- })
- });
- author.value = '';
- content.value = '';
- });
- }
- attachEvents();
Add Comment
Please, Sign In to add comment