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 7062813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:38:20+00:00 2026-05-28T04:38:20+00:00

I created a page on which there’s a red square that can be moved

  • 0

I created a page on which there’s a red square that can be moved by a mouse. I would like to find out how I can reuse Javascript to have more than 1 square. In the following fiddle http://jsfiddle.net/sbel/utM5k/ I show that in HTML on top I include the CSS, the square is a div and the Javascript has the form (function(window) {…})(window);. How can I change the Javascript to be able to simply say here is the ID of a div, apply the moving functionality to it?

  • 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-28T04:38:21+00:00Added an answer on May 28, 2026 at 4:38 am

    Rather than wrapping your functionality (variables and functions) in an anonymous closure (which is a good idea to avoid cluttering the global namespace), instead wrap them in a “class” definition so you can create multiple instances of your new type.

    For example (not tested):

    function MovableItem(id) {
      this.element = document.getElementById(id);
      this.mouseIsDownOnMovable = false;
      this.layerX = 0;
      this.layerY = 0;
      this.movableHeight = this.element.getBoundingClientRect().height;
      this.movableWidth = this.element.getBoundingClientRect().width;
      // And so on for each "member" variable...
    }
    
    MovableItem.prototype.markThatMouseIsDownOnMovable = function(event) {
      this.mouseIsDownOnMovable = true;
      this.layerX = event.layerX;
      this.layerY = event.layerY;
    }
    
    MovableItem.prototype.markThatMouseIsUp = function() {
      this.mouseIsDownOnMovable = false;
    }
    
    // And so on for each "method"...
    

    This refactoring will take some time as you will be learning some new concepts (especially regarding the use of the “this” variable) but when complete you will be able to have any number of your movable objects as such:

    var o1 = new MovableItem('slavaMovable');
    var o2 = new MovableItem('slavaMovable2');
    // ...
    

    This is the general way to achieve object oriented reusability in JavaScript (though the implementation specifics may vary greatly depending on your ideological preferences).

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

Sidebar

Related Questions

There are two scenarios for an ASP.net webforms page which I would like to
Here is my scenario. User fills out this large page which is dynamically created
I have searched and searched and I cannot find a page which spells out
i created a page on which i tried to attain the effect of bottom
I have a page which is largely created by DOM script, which generates a
I have a page which contains a jQuery-UI horizontal slider, created using a little
I've created a windows forms control, which is hosted in a web page viewable
I am new to objective-c and i have created a application in which there
I have created a jsp page which run fine when executed in tomcat.But when
I have created a web application in which there are two menus which load

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.