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

getelementbyID is not a function?

function printChores() {

for (let i =0; i< 7; i +=1) {   
var name = chores[Math.floor(Math.random() * chores.length)];
var task = chores[Math.floor(Math.random() * chores.length)];
document.getElementbyID('person').innerHTML = ('<p>' + 'Who: ' + name.Who + '' + '</p>');
document.getElementbyID('assignment').innerHTML = ('<p>' + 'Chore: ' + task.What + '' + '</p>');
} 
}

// This calls the function
printChores()

I'm trying to set this up so the information is dynamically created with javascript, but I keep getting errors that the document.getElementbyID is not a function. I've tried to assign the values you see here to them, as well as use them both within a variable. Nothing works.

3 Answers

You have clarified the function incorrectly. You have said:

document.getElementByID

/*you ended ID with a capital D in several places. This is incorrect, instead do the following:*/

document.getElementById('and specify your id here');

//Hope that helps. All the best.

Check out your casing, https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById

I haven't checked your code, but try running it again fixing your call to getElementById

Cheers!

Quick glance:

 // Have you defined a global array ?   
let chores = [];  

// Why are you using a for loop without the counter 'i' in your loop ?
for (let i = 0; i < 7; i++) { 
    i // 0, 1, 2, 3, 4, 5, 6
    // chores[i] ? do you need to loop through your global array?
}

// Are you trying to change DOM elements?( Or you want to create DOM elements?)
let person = document.getElementbyID('person');
person.innerHTML = 'string';

I have an array of objects I'm randomly grabbing info from. I wanted to loop through them and then on the 7th time through spit a different message out. I still have to work through the issues with that setup. I'm trying to change DOM elements.