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

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 02-05-2009, 06:57 PM   PM User | #1
Dan06
Regular Coder

 
Join Date: Sep 2008
Posts: 205
Thanks: 1
Thanked 0 Times in 0 Posts
Dan06 is an unknown quantity at this point
Question Curious case - code does not work without alert();

I am putting together a simple edit in place script. For some reason, in order for for the editEffect function I've created to work I need to use the alert function as seen in the code below. I can't figure out the cause of the problem and I believe the root cause is also creating other problems - i.e. in the cancelEdit function the argument "targetElement" is not alerted, but it is successfully submitted as an argument to another function. If anyone can see why I'm having the aforementioned problems, please let me know. Thanks.

Code:
/* Edit in place javascript code */
function editLightOn(element){
	element.style.backgroundColor = "#ffc";
}

function editLightOff(element){
	element.style.backgroundColor = "#FFFFFF";
}

function editable(element){
	if ( !document.getElementById("saveEdit") && !document.getElementById("cancelEdit") ){
		var targetElement = element;
		element.style.border = "3px inset black";
		var saveButton = document.createElement("span");
		var cancelButton = document.createElement("span");
		saveButton.style.display = "line-block";
		saveButton.innerHTML = '<input type="button" id="saveEdit" name="save" value="Save" />';
		cancelButton.innerHTML = '<input type="button" id="cancelEdit" name="cancel" value="Cancel" />';
		element.parentNode.insertBefore( saveButton, targetElement.nextSibling );
		element.parentNode.insertBefore( cancelButton, saveButton.nextSibling );
		var initialContent = targetElement.innerHTML;
	}
	if ( document.getElementById("saveEdit") && document.getElementById("cancelEdit") ){
		document.getElementById("cancelEdit").onclick = function(){ cancelEdit(targetElement, initialContent ); };
		document.getElementById("saveEdit").onclick = function(){ saveEdit(targetElement) };
	}
}

function nonEdit(element){
	element.style.border = "none";
	var saveButton = document.getElementById("saveEdit");
	var cancelButton = document.getElementById("cancelEdit");
	saveButton.parentNode.removeChild(saveButton);
	cancelButton.parentNode.removeChild(cancelButton);
}

function editEffect(){
	var editAreas = document.getElementsByTagName("span");
        alert(editAreas);
// If I remove the alert the remaining portion of this code seems to not work
	for ( var i = 0; i < editAreas.length; i++ ){
		editAreas[i].onmouseover = function(){ editLightOn(this); }
		editAreas[i].onmouseout = function(){ editLightOff(this); }
		editAreas[i].onclick = function(){ editable(this); }
		editAreas[i].onblur = function(){ nonEdit(this); }
	}
}

function saveEdit(targetElement){
	alert("saveEdit Function");
}

function cancelEdit(targetElement, initialContent){
	alert(targetElement);
	nonEdit(targetElement);
	targetElement.innerHTML = initialContent;
}
editEffect() is loaded via:
Code:
window.onload = function(){ editEffect(); };

Last edited by Dan06; 02-05-2009 at 09:58 PM..
Dan06 is offline   Reply With Quote
Old 02-05-2009, 07:12 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Try introducing a delay, thus:-

window.onload = function(){setTimeout("editEffect()", 200)}



All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
Philip M is offline   Reply With Quote
Old 02-05-2009, 07:32 PM   PM User | #3
Dan06
Regular Coder

 
Join Date: Sep 2008
Posts: 205
Thanks: 1
Thanked 0 Times in 0 Posts
Dan06 is an unknown quantity at this point
Quote:
Originally Posted by Philip M View Post
Try introducing a delay, thus:-

window.onload = function(){setTimeout("editEffect()", 200)}
I introduced the delay and the code now works without the use of alert(). But I don't understand why the delay was needed in addition to loading the function after the page is ready.
Dan06 is offline   Reply With Quote
Old 02-05-2009, 08:16 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Dan06 View Post
I introduced the delay and the code now works without the use of alert(). But I don't understand why the delay was needed in addition to loading the function after the page is ready.
Bug in IE. Glad your problem has been solved.
Philip M is offline   Reply With Quote
Old 02-05-2009, 09:57 PM   PM User | #5
Dan06
Regular Coder

 
Join Date: Sep 2008
Posts: 205
Thanks: 1
Thanked 0 Times in 0 Posts
Dan06 is an unknown quantity at this point
Quote:
Originally Posted by Philip M View Post
Bug in IE. Glad your problem has been solved.
Thanks for the help; I appreciate it.
Dan06 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 02:28 PM.


Advertisement
Log in to turn off these ads.