DOM & Data

Here is a rolodex with people's names and addresses:

We would like to run a function that will populate our page with names and addresses from an array of objects

Data

  • Add the array of objects

What we want to do is populate our page with data from the array. If, in the future, we change the data in the array, the page can be re-populated.

  • Write a loop that iterates over the array

  • We will now be interacting with the DOM so wrap the code in the DOMContentLoaded event listener.

  • Create a container that will house each name and address. Give it a class we can adjust later

  • Add in the name div, whose text comes from the array. Give it a class we can adjust later.

And the address div, whose text comes from the array. Give it a class we can adjust later.

  • Append the divs to the container div

Finished result

CSS

Give the body a nicer font

Give the info container some color, border, margin, padding

Last, give each name and address classes a display of inline-block, some margin, padding, and a border

Write in a function that will 'populate' the page

Now we can move the function out of the window onload, and just invoke the function within the DOMContentLoaded listener.

Adding data

  • Write a function that will push new data in to the array.

  • The function should take name and address as parameters

  • The function should then run the populateData function

The function 'doubles' the information. To fix this, we should clear out the old information before it populates.

Activity

See if you can figure out how to create a removeData function that takes a name of a person you want to remove, removes them from the data array, then refreshes the rolodex.

Last updated

Was this helpful?