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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Sep 2008
    Posts
    205
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Sep 2008
    Posts
    205
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    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.

  • #5
    Regular Coder
    Join Date
    Sep 2008
    Posts
    205
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Bug in IE. Glad your problem has been solved.
    Thanks for the help; I appreciate it.


  •  

    Posting Permissions

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