When I attempt this, the HMTL page only displays the last object, instead of all the objects.
Here is my JavaScript file
var family = {
aaron: {
name: 'Aaron',
age: 30
},
megan: {
name: 'Megan',
age: 40
},
aaliyah: {
name: 'Aaliyah',
age: 2
}
}
var list = function(family) {
for (var prop in family) {
var elList = document.getElementById('aaron-family').innerHTML = prop;
}
}
list(family);
And here's my HTML file
<html>
<head>
<title>Aaron's Test With Objects</title>
</head>
<li id="aaron-family">Hey, this is where it goes</li>
<footer>
<script src="objects.js"></script>
</footer>
</html>
Thank you guys! Here's the final solution I came up with:
JS
HTML
I'm sure it can be refactored but it works, visually.
Well, you've got a couple problems there (
<li>
tag without a parent<ol>
or<ul>
tag, among others)...but I'd say the primary error is that you are replacing each subsequent output with each assignment toinnerHTML
.Solution: assign a compiled array to
innerHTML
(usingjoin
to include spaces between the values)Remove
elList
because there is no point in having it...Then change
To
That way you are not constantly setting the
innherHTML
toprop
. Also, you might find it better to change the function to the following in order to prevent from constantly getting the element.Hope this helps:)
This dynamic way to render like this data format into HTML