0

I write a code that will arrange me a points sinside table, but when I specific arrayOfDivs all works great: http://jsfiddle.net/u58k6/6

var arrayOfDivs = [({topPosition : 99, leftPosition: 100}),({topPosition : 150, leftPosition: 400}),({topPosition : 578, leftPosition: 10})];

When I try to dinamicly creating this arrayOfDivs based on other things, when I add this code to create arrayOfDivs:

var arrayOfDivs = [];

$("#table .draggable").each(function(index, item){
   var lat = $(item).attr("lat");
   var lng = $(item).attr("lng");
   var top = $(this).position().top;
   var left = $(this).position().left;
   arrayOfDivs.push({topPosition : top, leftPosition: left, lat : lat, lng : lng});
});

var height = $('table').height();
var rowsCount = $('table tr').length;
var ROW_HEIGHT = height/rowsCount;

FINAL VERSION: http://jsfiddle.net/u58k6/10/ , I just get this error in console: Uncaught TypeError: Cannot call method 'push' of undefined

Why? What is the prob;em?

0

1 Answer 1

1

Your arrayOfDivs is being created successfully.

The TypeError is occurring on a line not shown in your question. It's here:

var rowNumber = Math.floor(position.topPosition / ROW_HEIGHT);
rows[rowNumber].push(position);

Because position.topPosition can be negative that means rowNumber can be negative, which means that you're trying to do rows[-1].push(position) which is essentially undefined.push(position).

(Add a console.log(rowNumber); statement just before the .push() and you'll see the -1.)

You don't have this problem when you hard-code the arrayOfDivs because none of the positions that you've specified are negative. It's when you get the positions of actual elements on the page that you might get negative numbers.

I don't have a specific suggestion for how to fix this, because I'm not (yet) sure what you're actually trying to achieve. But I hope that's enough for you to go on to fix it yourself.

Sign up to request clarification or add additional context in comments.

4 Comments

I try but again is the same Uncaught TypeError: Cannot call method 'push' of undefined
what I try to do is: arragnge divs in arrays , first by rows, so one row = one new array, after that arrange by left every new array created
please try to fix that
Well you could add if(rowNumber<0){ rowNumber = 0; } just after the var statement I quoted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.