Dan06
02-05-2009, 06: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(); };
/* 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(); };