Advertisement
dimoBs

Untitled

Nov 6th, 2021
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. async function solution() {
  2.  
  3. try {
  4. const url = 'http://localhost:3030/jsonstore/advanced/articles/list '
  5. const res = await fetch(url);
  6. const data = await res.json();
  7. console.log(data);
  8. data.forEach(info => {
  9. const getMoreInfo = addInfo(info._id);
  10. const divMainElement = document.createElement('div');
  11. divMainElement.innerHTML = `<div class="accordion">
  12. <div class="head">
  13. <span>${info.title}</span>
  14. <button class="button" id=${info._id}>More</button>
  15. </div>
  16. <div class="extra">
  17. <p>${getMoreInfo.content}</p>
  18. </div>
  19. </div>`
  20. document.querySelector('#main').appendChild(divMainElement)
  21. });
  22.  
  23. }catch (error) {
  24. console.log('Server error!');
  25. }
  26.  
  27. }
  28. document.querySelector('#main').addEventListener('click', btnOptions)
  29.  
  30. function btnOptions (ev) {
  31. if(ev.target.textContent == "More") {
  32. document.querySelector('.extra').style.display = "inline"
  33. ev.target.textContent = 'LESS'
  34. }
  35. }
  36.  
  37. async function addInfo(id) {
  38.  
  39. const url = `http://localhost:3030/jsonstore/advanced/articles/details/${id}`
  40. const res = await fetch(url);
  41. const data = await res.json();
  42. return data;
  43. }
  44. solution();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement