How do i make a allocation table?

Posted by david on Stack Overflow See other posts from Stack Overflow or by david
Published on 2010-04-12T15:15:54Z Indexed on 2010/04/16 7:33 UTC
Read the original article Hit count: 335

Filed under:
|

I have build a grid of div's as playground for some visual experiments. In order to use that grid, i need to know the x and y coordinates of each div. That's why i want to create a table with the X and Y position of each div.

X:0 & Y:0 = div:eq(0), X:0 Y:1 = div:eq(1), X:0 Y:2 = div:eq(2), X:0 Y:3 = div:eq(3), X:1 Y:0 = div:eq(4) etc..

What is the best way to do a table like that? Creating a OBJECT like this:

{ 00: 0, 01: 1, 02: 2, etc.. }

or is it better to create a array?

position[0][0] = 0

the thing is i need to use the table in multiple way's.. for example the user clicked the div nb: 13 what are the coordinates of this div or what is the eq of the div x: 12 y: 5.

Thats how i do it right now:

        var row = 0
    var col = 0
    var eq = 0      

    c.find('div').each(function(i){ // c = $('div#stage')

        if (i !=0 && $(this).offset().top != $(this).prev().offset().top){
            row++
            col = 0
        }

        $(this).attr({'row': row, 'col': col })

        col++

    })

I think it would be faster to build a table with the coordinates, instead of adding them as attr or data to the DOM. but i cant figure out how to do this technically.

How would you solve this problem width JS / jQuery?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about JavaScript