Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6120445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:40:09+00:00 2026-05-23T15:40:09+00:00

I am new to javascript and I am trying to start a very simple

  • 0

I am new to javascript and I am trying to start a very simple project which is to display a controllable div that can be moved around using a,w,s,d keys on keyboard. I am currently having problem on how to move around the div because I do not know what attribute to change.

divBar = null;

function detectKey() {
    //97 = a
    //115 = s
    //100 = d
    //119 = w

    if (event.charCode == 97) {
       //a
       alert(divBar.position);
    }
    if (event.charCode == 115) {
        //s
    }
    if (event.charCode == 100) {
        //d
    }
    if (event.charCode == 119) {
        //w
    }
}

function createDiv() {
    divBar = document.createElement("div");
    divBar.id = "divBar";
    divBar.style.border = "solid 1px #AAAAAA";
    divBar.style.backgroundColor = "black";
    divBar.style.top = 400;
    divBar.style.height = "10px";
    divBar.style.width = "100px";
    divBar.style.position = "absolute";
    document.body.appendChild(divBar);
    document.addEventListener("keypress", detectKey, false);

}

I am not sure to put in that condition statement. so that the div will move to the left, right, up and down.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:40:09+00:00Added an answer on May 23, 2026 at 3:40 pm

    If it’s absolutely positioned (which it appears to be), then you change divbar.style.top and divbar.style.left to move it around.

    Here’s a working example: http://jsfiddle.net/jfriend00/rRbZz/.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to javascript and have been playing around with regular expressions. I'm
i'm relatively new to jquery and javascript and am trying to pass a unique
I'm completely new to the javascript and ajax world but trying to learn. Right
I am trying to create a new Element in my javascript code and append
I'm somewhat new to object oriented programming in Javascript and I'm trying to build
I'm really new to ASP.NET MVC, and I'm trying to integrate some Javascript into
Hey there, new to AJAX, JS.. Im trying to add to a Javascript/Ajax search
Using Embedded Jetty I am trying to get a very simple servlet to forward
I am trying to get get zombie.js to activate a link that uses javascript.
hello im trying to make something like github treeslider im new to javascript.. but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.