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
Macauley Morgan
2,017 PointsWhy is this not working?
var $mockItems = document.getElementsByClassName( "mock-item" ); for ( var i = 0; $mockItems.length > i; i++ ) { $mockItems[i].addEventListener( "click", mockChange ); }; function mockChange() { console.log("exe"); }; This set of JavaScript won't send the string into the console, obviously its not my end-game aim but it just won't function the way I want it to
4 Answers
Clayton Tevebaugh
7,617 PointsI am not really far enough to know why it isn't working, but I will repost your code so someone who might can take a look at see it sooner.
var $mockItems = document.getElementsByClassName( "mock-item" );
for ( var i = 0; $mockItems.length > i; i++ ) {
$mockItems[i].addEventListener( "click", mockChange );
};
function mockChange() {
console.log("exe");
};
Daniel Newman
Courses Plus Student 10,715 Pointsfor ( var i = 0; $mockItems.length > i; i++ ) {
It will fall because even in the first step "$mockItems.length > i" is false. Everything else looking good enough.
Marcus Parsons
15,719 Points"$mockItems.length > i" is not false by any means. All this statement is is "i < $mockItems.length" reversed with the proper comparison sign. It works.
Marcus Parsons
15,719 PointsHi Macauley,
There's nothing wrong with the code you posted. I went ahead and tested it in a codepen and it works as expected: http://codepen.io/anon/pen/YXZEoo. Are you getting any errors in your console when you open it up?
I'm thinking there is something else going on that is preventing the code from executing. If you're working on this in Workspaces, the easiest way to share is posting a snapshot of your workspace so that we have access to the files you're working on. Here's how to do that: http://www.teamtreehouse.com/forum/workspace-snapshots
If not, you might try dumping your entire code into a codepen at http://www.codepen.io so that we can see what's going on.
Macauley Morgan
2,017 Pointshttp://codepen.io/anon/pen/aOJjLE Is my JS file because I'm not using work spaces right now
Marcus Parsons
15,719 PointsNot just your JavaScript but your HTML, as well. It does no good to just have the JS. Dump the HTML into the codepen and then hit save, man haha
Macauley Morgan
2,017 Pointshttp://codepen.io/anon/pen/QbpJay
Wasn't fully functioning this morning haha
Marcus Parsons
15,719 PointsWhat is your goal with this project? It just really seems like you're doing a fairly simple task with overly complex code. I can help you greatly simplify if I know what the aim is of this project.
Macauley Morgan
2,017 PointsI'm trying to make an app that someone can upload an image to and the image gets mocked up onto an object such as a wall.
Macauley Morgan
2,017 PointsThis would be an automated process if you get what I mean
Marcus Parsons
15,719 PointsI see what you mean, but just to clarify, you would have a variety of background images (or this is dynamic too) and the mock images that are imported into the application (and perhaps some stock ones already available) would display on top of these background images, right?
Macauley Morgan
2,017 PointsYeah that is what I mean, they would be uploaded to the mocker by the user to see their designs,sorry I took so long, couldn't get online because of a technological fault, there would originally be a photo of a wall or something along those lines and with a png or illustrator file uploaded to it, would offer a preview