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

getting a box to move left and right javascript/jquery

guys im trying to get my box to move left or right but its popping an error: Uncaught TypeError: Cannot set property 'right' of undefined.

obviously have trouble with the css properties, but what property would work?

Here's code:

$("document").ready(function() {

console.log("all good so far")
//set the click function to alert keycode

var box = $("#box");
var container = $("container")
var boxRight = 0;
var move = function(e){
alert(e.which);
var code = e.which;

    if(code == 39){
        boxRight+= 2;
        box.style.right = boxRight + "px";

    }
}

$(document).keydown(move);

});

1 Answer

Just realize i needed the var box = $("#box")[0];