Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDinesh Kumaar Rajendran
3,395 PointsInner HTML doesn't work, Please help
HTML
<script src="script.js"></script>
<div id="output">
</div>
JS
var students =[
{name:"Mr A",
track:"android development"
,achievement:510
,points_earned:53000
},
{name:"Mr B",
track:"ios development"
,achievement:150
,points_earned:50003
},
{name:"Mr C"
,track:"Python development"
,achievement:350,
points_earned:50040
},
{name:"Mr D",
track:"Frontend development"
,achievement:250
,points_earned:50800
},
{name:"Mr E"
,track:"Backend development"
,achievement:50
,points_earned:50700
}
];
var message = "";
var student;
for (var i =0; i<students.length;i++){
student = students[i];
message += '<h2>'+'Student name is '+ students[i].name +'</h2>'
}
function print(message){
var outputDiv = document.getElementById("output");
outputDiv.innerHTML = message;
}
print(message);
/*whereas document.write works well*/
1 Answer
Seth Kroger
56,413 PointsYou should put the script tag after the div as the last item of the HTML. Then it should work fine.
One of gotchas with including JavaScript with HTML is that it can only see DOM elements after they've been loaded in, and this happens in the order they're read. When you just execute straight it won't see the output div because it comes after the script. This is why it is common convention to place script tags at the end of the body, so all the DOM elements from the HTML are loaded by that point.
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsBrendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsWhat are you expecting to happen? I ran your code and this is what I see in the output:
Student name is Mr A
Student name is Mr B
Student name is Mr C
Student name is Mr D
Student name is Mr E