Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10

Thread: help with array

  1. #1
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts

    help with array

    Hi, im new to the javascript side of things, could someone point out where im going wrong in this array:

    Code:
    $("*").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
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    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) 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:
    Code:
    elements[i] = elements[i]||[]; // here's the magic
    elements[i]['id'] = $(this).attr("id");
    /* and everything else */
    Are you a Help Vampire?

  • Users who have thanked tomws for this post:

    Phil Jackson (12-11-2009)

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    You must define the array as an array of arrays
    Code:
    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
    Code:
    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();
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • Users who have thanked Kor for this post:

    Phil Jackson (12-11-2009)

  • #4
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    still no such luck:

    error: elements is not defined
    file:///C:/wamp/www/test/test.html
    Line 67
    elements[i]=[];\r\n
    Code:
    $("*").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();
    	});
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #5
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    You removed some code:
    Code:
    var elements = new Array(5);
    Are you a Help Vampire?

  • #6
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    got it cheers guys

    Code:
    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));
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #7
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    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?
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by Phil Jackson View Post
    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.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #9
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    First time i've wrote one of these:

    Code:
    $.fn.parentNumber = function(){
    	var i = 0;
    	$(this).parents().each(function(){
    		i++;					 
    	});
    	return Number(i) - 1;
    }
    think it's ok
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #10
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    $(this).parents().length?
    Are you a Help Vampire?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •