SheTech
  • SheTech JS bootcamp
  • Level 1 - awesome tasks
    • Introduction to Awesome Tasks
    • Step 1: HTML
    • Step 2: playing with the DOM
    • Step 3: add a new task
    • Step 4: dynamic list of tasks
    • Step 5: adding new items
    • Step 6: removing items
    • Step 7: add some style
    • Step 8: marking items as done
    • Step 9: saving items in the localstorage
    • Step 10: adding filters and bulk actions
    • Step 11: add counters
    • From Awesome Task to the next big thing!
  • Level 2 - timeline
    • Live Timeline
    • Step 1: basic HTML page
    • Step 2: add util library
    • Step 3: show all posts
    • Step 4: add form
    • Step 5: collect form data
    • Step 6: create a new post
    • Step 7: validating posts
    • Step 8: refactoring
    • Step 9: refresh button
    • Step 10: auto refresh
  • Code Editors
  • Useful resources
  • API Reference
  • Glossary
Powered by GitBook
On this page

Was this helpful?

  1. Level 2 - timeline

Step 9: refresh button

Refreshing the post list

Thanks to code we added in the previous step, we only refresh the list when a user posts a new message.

Let's add a button to manually refresh the whole list.

8.1 – The refresh button's HTML

In the head add:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.3.0/font/bootstrap-icons.css">

And right after the <body> tag add:

<button type="button" class="btn btn-secondary" id="refresh-button" style="position: fixed; top: 0; right: 0;">
  <i class="bi-arrow-repeat" style="font-size: 2rem;"></i>
</button>

In order to give the button a nicer look, we make use of Bootstrap icons.

The inline style anchors the button to the top-right corner of the screen, even when the page is scrolled.

8.2 – The refresh button's javascript

Let's add a console.log() statement in the refreshMessages function, so that we can see the function being called when the user clicks the button.

console.log('refresh');

Let's move now to the end of the script, where we add the click handler to the button.

First of all, we get the reference to the refresh button with the good old document.querySelector:

var refreshButton = document.querySelector('#refresh-button');

Then we define the handler for the click event, in a similar way we did for the form's submit event. Inside the handler function we simply call the refreshMessages function.

refreshButton.onclick = function() {
  refreshMessages();
}
PreviousStep 8: refactoringNextStep 10: auto refresh

Last updated 4 years ago

Was this helpful?