...

View Full Version : Resolved Curious case - code does not work without alert();



Dan06
02-05-2009, 07:57 PM
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.



/* 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:


window.onload = function(){ editEffect(); };

Philip M
02-05-2009, 08:12 PM
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.

Dan06
02-05-2009, 08:32 PM
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.

Philip M
02-05-2009, 09:16 PM
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.

Dan06
02-05-2009, 10:57 PM
Bug in IE. Glad your problem has been solved.

Thanks for the help; I appreciate it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum