CyanPrime
CyanPrime

Reputation: 5193

Move a div using keypresses and jquery?

Alright, so I'm trying to make a simple script that moves down a div when I press 's' but it's not firing at all. I got Jquery installed, and the error-console in FF isn't saying anything. Can someone tell me what I'm doing wrong?

http://prime.programming-designs.com/designs/map/

Upvotes: 1

Views: 484

Answers (3)

mVChr
mVChr

Reputation: 50177

Here's your code:

$('#map').keydown(function(event) {
    switch (event.keycode) {
        case 83: // left arrow key
            markX = markX + markSpeed;
            mark.style.top  = markX + 'px';
            break;
    }
});

The issues are that keycode needs to be camelCase keyCode and that the case should be 37 for the left arrow or 40 for the down arrow.

Upvotes: 1

zzzzBov
zzzzBov

Reputation: 179046

You forgot to put // before your HTML open comment tag <!--

You shouldn't use HTML comment tags for scripts anyway, the best method for inline styles/scripts is to use the CDATA element as follows:

/* <![CDATA[ */
//your code in here
/* ]]> */

This does a number of things:

  • allows XML entities to not need to be escaped
  • use of multi-line comments prevents any newline issues that may result in your script being parsed (CMS's will often adjust white-space)
  • creates valid XML when used in XHTML

Upvotes: 1

Joel Martinez
Joel Martinez

Reputation: 47749

I suggest you use the jquery.hotkeys extension to bind to the key events: https://github.com/jeresig/jquery.hotkeys

Upvotes: 1

Related Questions