...

View Full Version : help with array



Phil Jackson
12-11-2009, 02:15 PM
Hi, im new to the javascript side of things, could someone point out where im going wrong in this array:



$("*").each(function(i){
var elements = new Array(5);
if($(this).attr("class")!="nbta"){
var borderStyle = $(this).css("border");
if(borderStyle==""){
$(this).attr("style", "border:3px solid transparent;");
}
}

elements[i]['id'] = $(this).attr("id");
elements[i]['x'] = Left($(this));
elements[i]['y'] = Top($(this));
elements[i]['width'] = $(this).width();
elements[i]['height'] = $(this).height();
});
alert(dump(elements));


firebug says -

elements[i] is undefined
file:///C:/wamp/www/test/test.html
Line 67

tomws
12-11-2009, 03:00 PM
That's easy: it's undefined. :)

I had this problem recently. Javascript doesn't handle undefined indexes the same as PHP. If the index does not exist before attempting to access the location, you get that error whereas PHP just creates it and moves along.

The solution (as I discovered starting here (http://www.codingforums.com/showpost.php?p=882875&postcount=4)) is to create the location if it's not in use yet. Fairly simple once you see it. I think it would be something like this:

elements[i] = elements[i]||[]; // here's the magic
elements[i]['id'] = $(this).attr("id");
/* and everything else */

Kor
12-11-2009, 03:01 PM
You must define the array as an array of arrays


elements[i]=[];
elements[i]['id'] = $(this).attr("id");
elements[i]['x'] = Left($(this));
elements[i]['y'] = Top($(this));
elements[i]['width'] = $(this).width();
elements[i]['height'] = $(this).height();

Or you can define it as an array of objects


elements[i]={};
elements[i]['id'] = $(this).attr("id");
elements[i]['x'] = Left($(this));
elements[i]['y'] = Top($(this));
elements[i]['width'] = $(this).width();
elements[i]['height'] = $(this).height();

Phil Jackson
12-11-2009, 04:30 PM
still no such luck:


error: elements is not defined
file:///C:/wamp/www/test/test.html
Line 67
elements[i]=[];\r\n



$("*").each(function(i){

if($(this).attr("class")!="nbta"){
var borderStyle = $(this).css("border");
if(borderStyle==""){
$(this).attr("style", "border:3px solid transparent;");
}
}

elements[i]=[];
elements[i]['x'] = Left($(this));
elements[i]['y'] = Top($(this));
elements[i]['width'] = $(this).width();
elements[i]['height'] = $(this).height();
});

tomws
12-11-2009, 04:31 PM
You removed some code:

var elements = new Array(5);

Phil Jackson
12-11-2009, 04:38 PM
got it cheers guys


var elements = new Array();
$("body *").each(function(i){

if($(this).attr("class")!="nbta"){
var borderStyle = $(this).css("border");
if(borderStyle==""){
$(this).attr("style", "border:3px solid transparent;");
}
}
elements[$(this).attr("id")]=[];
elements[$(this).attr("id")]['x'] = Left($(this));
elements[$(this).attr("id")]['y'] = Top($(this));
elements[$(this).attr("id")]['width'] = $(this).width();
elements[$(this).attr("id")]['height'] = $(this).height();
});
alert(dump(elements));

Phil Jackson
12-11-2009, 04:45 PM
Thax all, also something else thats bothering me. Is there a function for returning the amount of parents an element has or do i need to write my own?

Kor
12-11-2009, 04:57 PM
Thax all, also something else thats bothering me. Is there a function for returning the amount of parents an element has or do i need to write my own?
You need to write it on your own.

Phil Jackson
12-11-2009, 05:13 PM
First time i've wrote one of these:



$.fn.parentNumber = function(){
var i = 0;
$(this).parents().each(function(){
i++;
});
return Number(i) - 1;
}


think it's ok

tomws
12-11-2009, 06:05 PM
$(this).parents().length?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum