Thread: Edit-in-place
View Single Post
Old 04-15-2009, 03:45 PM   #1 (permalink)
Tanax
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default Edit-in-place

Hiya!

I'm having some troubles with a javascript. It's supposed to be like .. when you click an element that has class "editable", text should be replaced with textarea and allow you to edit the text.

Right now it doesn't save it to a database or anything, I'm just trying to figure out how to accomplish this. And no - I'm not using any js framework.

Looks like this:
Code:
var config = {
	rows: '5',
	cols: '25',
	okButtonText: 'Redigera',
	cancelButtonText: 'Avbryt'
};

window.onload = function  () {
	var pElements = document.getElementsByTagName('*');
	for (var i=0; i < pElements.length; i++) {
		if(pElements[i].className == 'editable')
			inPlaceEditor(pElements[i]);
	};
};

function inPlaceEditor (editableElement) {

	editableElement.onclick = function(e) {
	
		if(this.childNodes[1] == undefined)
		{

			var text = getTextArea(this);
			var buttons = getButtons(text.div);

			this.className = 'not_editable';

		}

		if(buttons)
		{

			var container = this;
			buttons.cancel.onclick = function() {

				container.firstChild.nodeValue = text.origText;
				alert(container.childNodes[1]);

				container.className = 'editable';

			}

			buttons.edit.onclick = function() {

				container.className = 'editable';
				alert('You clicked edit!');

			}


		}

	}

}

function getTextArea(obj)
{

	var origText = obj.firstChild.nodeValue;
	obj.firstChild.nodeValue = '';

	var div = document.createElement("div");
	obj.appendChild(div);

	var textarea = document.createElement("textarea");

	textarea.setAttribute("cols", config.cols);
	textarea.setAttribute("rows", config.rows);
	textarea.value = origText;

	div.appendChild(textarea);

	var text = {
		'origText': origText,
		'textarea': textarea,
		'div': div
	}

	return text;

}

function getButtons(obj)
{

	var br = document.createElement("br");
	var edit = document.createElement("input");
	
	edit.setAttribute("type", "button");
	edit.setAttribute("value", config.okButtonText);

	obj.appendChild(br);
	obj.appendChild(edit);

	var cancel = document.createElement("input");
		
	cancel.setAttribute("type", "button");
	cancel.setAttribute("value", config.cancelButtonText);
		
	obj.appendChild(cancel);

	var buttons = {
		'edit': edit,
		'cancel': cancel
	}

	return buttons;

}
So the problem is, I can click an element, it removes the text, adds a div element, and inside the new div it adds a textarea, br, and 2 input buttons. Right now I'm coding the "cancel" button, which will just cancel the changes you've made and remove the textarea and input buttons.

As you see I'm alerting container.childNodes[1] - which is the div element, but I can't seem to be able to remove it by container.removeChild(container.childNodes[1]); which is really weird?

Also, after I've "edited" an element, it doesn't work to click on that element again to get the textarea again.. how come?

If you need HTML source to be able to help, post and I will post it!
Thanks
__________________
Tanax is offline  
Reply With Quote