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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:02:18+00:00 2026-05-13T15:02:18+00:00

I have an javascript array, which looks like: var styles = new Array(); styles[0]

  • 0

I have an javascript array, which looks like:

var styles = new Array();
styles[0] = { ... Position: 0, ... };
styles[1] = { ... Position: 1, ... };
styles[2] = { ... Position: 2, ... };
...
styles[N] = { ... Position: N, ... };

I use this array to display a list, where each item is a div. The end result is this:

<div id="container">
 <div>... item 1...</div> 
 <div>... item 2...</div> 
 <div>... item 3...</div> 
</div>

Now the “container” div is also jquery sortable. That way I can drag/drop the items and change the position. Now whenever the user drags an item to a different position I update the positions back in the array by looping through the div items, which is pretty bad. It looks more or less like that:

var items = $("#container");
for (var i = 0; i < items.length; i++)
{
    ....
    styles[i] = { ... Position: i, ... };
}

Is there a better way to achieve this?

Update 1:

I need to save the positions in the database, which is why I need change my array after the list has been changed. The list changes depending on other criterias. So I could have a list of 10 items or I could have a list of X items. It depends on which list the users selects. Now if the user changes one list and then wants to see a second list, then I need to make sure that the first list maintains the positions.

  • 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-13T15:02:18+00:00Added an answer on May 13, 2026 at 3:02 pm

    I think Drew Wills response is on the mark, and this might also help. A div is a JavaScript object, which means you can add properties to it. So you might be able to reduce the number of lines of JavaScript code and increase the code’s expressiveness by sticking your “information object” right onto each div, meaning you’d no longer need your array.

    Where does your array come from in the first place? I’m going to assume it is a JSON-ified version of some data you have on the server. So I’m assuming (guessing) that you have some kind of “each” or for loop that creates the divs from the array, perhaps like this:

    for (var i = 0; i < styles.length; i++)
    {
        var newDiv = $("<div>" + style[i].item + "</div>";
        $("#container").append(newDiv);
    }
    

    Assuming you have that, then you could modify it to this:

    for (var i = 0; i < styles.length; i++)
    {
        var newDiv = $("<div>" + style[i].item + "</div>";
    
        // KEY NEW LINE FOLLOWS:
        newDiv.myCustomObject = styles[i];
    
        $("#container").append(newDiv);
    }
    

    Now, as the divs get sorted all over the place, this “myCustomObject” goes along with them. You don’t need the array anymore. If you need the “Position” property on “myCustomObject” to be updated, use the “index” property of the div, as Drew Wills said.

    So, imagine that your “styles” object has a property in it called “Color”. Imagine you want to show an alert on click that tells the color associated with the div they clicked on. You can now accomplish this without needing the “styles” array, because the divs now have everything you need. So you could do this:

    $("#container div").click(function() {
       alert("The color is " + this.myCustomObject.Color);
    });
    

    Later, when it comes time to post or send via ajax the positions, you could do this:

    $("#container div").each(function(index) {
       this.myCustomObject.Position = index;
    }
    

    NOTE: Also, some people would prefer to use the JQuery data() method to associate “myCustomObject” with the div. It achieves the same result and perhaps gets garbage collected more completely in some browsers.

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

Sidebar

Related Questions

I have javascript code some thing like this -- var count=3; var pl=new Array(count);
I have an ordered array which looks like this:- [-0.0020057306590257895, 50, 0.09598853868194843, 50, 0.19398280802292264,
I have an object in my javascript which looks like this: {data:[{t:{ level:35, longtitude:121.050321666667,
1) I have this Javascript array: lang=new Array(); lang[sq]=Albanian; lang[ar]=Arabic; lang[en]=English; lang[ro]=Romanian; lang[ru]=Russian; 2)
I have an array in Javascript: var array = new array(); array[0] = apples;
In JavaScript Language , I have an array ... maybe it look like this
I have an empty array into which I am pushing values using javascript. I
I have a javascript array: var exclude = [Santorum,Obama,Romney,Gingrich]; I have html links: <a
for example: I have created one javascript property array model with some properties like
I have an array of JavaScript objects: var objs = [ { first_nom: 'Laszlo',

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.