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

Why 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

I 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"); 
};
for ( 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.

"$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.

Hi 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.

http://codepen.io/anon/pen/aOJjLE Is my JS file because I'm not using work spaces right now

Not 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

http://codepen.io/anon/pen/QbpJay

Wasn't fully functioning this morning haha

What 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.

I'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.

This would be an automated process if you get what I mean

I 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?

Yeah 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