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 trial

JavaScript

I'm struggling with the `document.getElementById` function. Please help!

I was able to get the document.print method to work but am struggling with document.getElementById. Nothing prints for me.

Here is my code for this tutorial

// Quiz

var quiz = [
  ['What day is it?', 'Wednesday'],
  ['What city are we in?', 'Denver'],
  ['What sport is tonight?', 'Soccer'],
  ['What am I learning?', 'Javascript']
];

// Variables

var correctAnswers = 0;
var question;
var answer;
var response;
var html;
var correct = [];
var wrong = [];

// Functions

function print(message) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;
}

function buildList(arr) {
  var listHTML = '<ol>';
  for (var i = 0; i < arr.length; i += 1) {
    listHTML += '<li>' + arr[i] + '</li>';
  }
  listHTML += '</ol>'
  return listHTML;
}

// Loops

for (var i = 0; i < quiz.length; i += 1) {
  question = quiz[i][0];
  answer = quiz[i][1];
  response = prompt(question);
  if (response === answer) {
    correctAnswers +=1;
    correct.push(question);
  } else {
    wrong.push(question);
  }
}

// Print it out

html = "you answered " + correctAnswers + " question(s) correct."
html += '<h2> You got these questions correct: '+ correct '</h2>';
html += buildList(correct);
html += '<h2> You got these questions wrong: '+ wrong '</h2>';
html += buildList(incorrect);

print(html);
Ken Alger
Ken Alger
Treehouse Teacher

Eric;

Welcome to Treehouse!

I don't see your code posted. When you get a chance post it and I'm sure you will get a resolution to your issue.

Happy coding,

Ken

3 Answers

Hi Ken,

it should be there now. Thx!

Eric

uh? javascript doesn't have print method its

document.write();

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_doc_write

get element by id is getting id selector aka

<style>
    #id {
color: red;
    }
</style>
<body>
<p id="id">red color</p>

I hope that helps.

My bad! I was using document.write() prior to getElementById and everything was being written fine.

When I changed to the getElementById method (with a div using id='output' in the HTML)... Nothing is is showing up in the preview.

Any ideas? Thx in advance. E