Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-28-2009, 07:29 PM   PM User | #1
Phil Jackson
Senior Coder

 
Join Date: Aug 2009
Location: Mansfield, Nottinghamshire, UK
Posts: 1,547
Thanks: 57
Thanked 148 Times in 147 Posts
Phil Jackson is on a distinguished road
my own accord script

Howdy do. I have the following code which, all works apart from setting height when clicked on. error: object is indefined

anyonw know where im going wrong?

Code:
// JavaScript Document
// Created by ACT WEb Designs
// http://www.actwebdesigns.co.uk
//
//<div id="reportWrapper">
//	<div class="dropDiv">
//		<p><a class="open">open me</a></p>
//		<p> blablabla blablabla</p>
//		<p><a class="close">close me</a></p>
//	</div>
//	<div class="dropDiv">
//		<p><a class="open">open me</a></p>
//		<p> blablabla blablabla</p>
//		<p><a class="close">close me</a></p>
//	</div>
//</div>

	function dropBox() {
		function anchorReplace()
		{
			$("a[class=open]").each(function(i){
				var anchorElement = $(this);
				var newAnchorElement = $('<a href="#link' + i + '" class="' + anchorElement.attr('class') + '" name="#link' + i + '">' + anchorElement.text() + '</a>').insertBefore(anchorElement);
					anchorElement.remove();
			});
			$("a[class=close]").each(function(i){
				var anchorElement = $(this);
				var newAnchorElement = $('<a href="#link2' + i + '" class="' + anchorElement.attr('class') + '" name="#link2' + i + '">' + anchorElement.text() + '</a>').insertBefore(anchorElement);
					anchorElement.remove();
			});
		}
		function giveDivName() //simply add name attribute to div element
		{
			$("div[class=dropDiv]").each(function(i){ //foreach div with class dropDiv
				var divElement = $(this);
				var newDivElement = $('<div class="dropDiv" name="div' + i + '">' + divElement.html() + '</div>').insertBefore(divElement);
					divElement.remove();
			});	
		}
		function getOrigValues() //get the values before closing div 
		{
			var boxInfo = []; //get original div height
			$("div[class=dropDiv]").each(function(i){	
				var height = $(this).height(true);
				var name = $(this).attr("name");
				var obj = { boxHeight: height, boxName: name };
				return  boxInfo.push(obj);
			});	
			$("div[class=dropDiv]").css("height", "27px"); //close all dropDiv's
		}
		
		anchorReplace();
		giveDivName();
		var boxInfo = getOrigValues();
		
		$("#reportWrapper div").css("overflow", "hidden");
		$("a[class=close]").bind("click", function(){
			$(this).parents("div:eq(0)").animate({
				height: '30px'
			}, 1000);					 
		});
		$("a[class=open]").bind("click", function(){
			var clicked = $(this);
			$("a[class=open]").each(function(){	
				if( clicked.attr("name") !== $(this).attr("name") )
				{
					$(this).parents("div:eq(0)").animate({
						height: '30px'
					}, 1000);
				}
				else
				{
					var element = $(this);
					function getNewHeight(element, boxInfo) // get the height for when clicked to open
					{
						var divName = element.parents("div:eq(0)").attr("name");
						$.each(boxInfo,function(i,n) {
							if( n.boxName == divName )
							{
								var newHeight = n.boxHeight;
							}
						});
	
					}
					getNewHeight(element, boxInfo);
					clicked.parents("div:eq(0)").animate({
						height: newHeight + 'px'
					}, 1000);
				}
			});
		});
	}
__________________
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(); 
Phil Jackson is offline   Reply With Quote
Old 09-28-2009, 08:20 PM   PM User | #2
Phil Jackson
Senior Coder

 
Join Date: Aug 2009
Location: Mansfield, Nottinghamshire, UK
Posts: 1,547
Thanks: 57
Thanked 148 Times in 147 Posts
Phil Jackson is on a distinguished road
narrowed it down a bit:

Code:
var element = $(this);
var divName = element.parents("div:eq(0)").attr("name");
$.each(boxInfo,function(i,n) {
    if( n.boxName == divName )
    {
        var newHeight = n.boxHeight;
    }
});

clicked.parents("div:eq(0)").animate({
    height: newHeight + 'px'
}, 1000);
Problem being "newHeight undefined". But if i do this:

Code:
var element = $(this);
var divName = element.parents("div:eq(0)").attr("name");
$.each(boxInfo,function(i,n) {
    if( n.boxName == divName )
    {
        var newHeight = n.boxHeight;
        alert(newHeight); // output being 230
    }
});

clicked.parents("div:eq(0)").animate({
    height: newHeight + 'px'
}, 1000);
it returns the height. How is it that 5 lines down the variable is undefined??
__________________
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(); 
Phil Jackson is offline   Reply With Quote
Old 09-28-2009, 08:23 PM   PM User | #3
Phil Jackson
Senior Coder

 
Join Date: Aug 2009
Location: Mansfield, Nottinghamshire, UK
Posts: 1,547
Thanks: 57
Thanked 148 Times in 147 Posts
Phil Jackson is on a distinguished road
sussed it

Code:
                                        var element = $(this);
					var divName = element.parents("div:eq(0)").attr("name");
					$.each(boxInfo,function(i,n) {
						if( n.boxName == divName )
						{
							var newHeight = n.boxHeight;
							clicked.parents("div:eq(0)").animate({
								height: newHeight + 'px'
							}, 1000);
						}
					});
__________________
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(); 
Phil Jackson is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:14 PM.


Advertisement
Log in to turn off these ads.