Results 1 to 5 of 5

Thread: Function to edit a <p> tag

  1. #1
    Join Date
    Feb 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Function to edit a <p> tag

    I have a form when submitted the values are captured on a PHP file. I have added the function below to edit the values. The function works but when you save the file are re-open the changes have not been saved. Is there any chance this can be done in JavaScript without server side scripts?

    Code:
    var editing  = false;
    
    if (document.getElementById && document.createElement) {
    	var butt = document.createElement('BUTTON');
    	var buttext = document.createTextNode('Save!');
    	butt.appendChild(buttext);
    	butt.onclick = saveEdit;
    }
    
    function catchIt(e) {
    	if (editing) return;
    	if (!document.getElementById || !document.createElement) return;
    	if (!e) var obj = window.event.srcElement;
    	else var obj = e.target;
    	while (obj.nodeType != 1) {
    		obj = obj.parentNode;
    	}
    	if (obj.tagName == 'TEXTAREA' || obj.tagName == 'A') return;
    	while (obj.nodeName != 'P' && obj.nodeName != 'HTML') {
    		obj = obj.parentNode;
    	}
    	if (obj.nodeName == 'HTML') return;
    	var x = obj.innerHTML;
    	var y = document.createElement('TEXTAREA');
    	var z = obj.parentNode;
    	z.insertBefore(y,obj);
    	z.insertBefore(butt,obj);
    	z.removeChild(obj);
    	y.value = x;
    	y.focus();
    	editing = true;
    }
    
    function saveEdit() {
    	var area = document.getElementsByTagName('TEXTAREA')[0];
    	var y = document.createElement('P');
    	var z = area.parentNode;
    	y.innerHTML = area.value;
    	z.insertBefore(y,area);
    	z.removeChild(area);
    	z.removeChild(document.getElementsByTagName('button')[0]);
    	editing = false;
    }
    document.onclick = catchIt;

  2. #2
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default

    You could use cookies, but those aren't great for storing anything important, because the expire and people can delete them and the client can change them. I actually have no idea what you asking but I hope this helps anyway. Also, what does this have to do with a P tag?

  3. #3
    Join Date
    Feb 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    The function I posted edits <p> tag.

    Can you give me a simple example of using a cookie for my problem please?

  4. #4
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default

    Ok, first of all, your code only seems to work in IE. Unless this is for personal use, this really wont work. Second, it seems very roundabout, and I don't even see what you trying to do. Explain, please. From what I can see, you have a textbox. Pressing a button destroys the button and the textbox, and creates a P with the textbox's content. Then if you click on the page again it goes back to the textbox and button. If this is really what is happening, why do you expect it so save it in between refreshes? I'm rather confused.

  5. #5
    Join Date
    Feb 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Didn't realise the code only works in IE (that's not a problem though).

    What the code allows me to do is edit the value captured from a textbox. The file the values are displayed is a PHP file. The value is wrapped in a <p>.
    Code:
    <strong>Details: </strong><p><?php echo $_POST["details"]; ?></p><br />
    When you click on the text it changes to a textbox (which you can edit). Next to the textbox is a button which allows you to save your changes (which it does). However when I save the page and then re-open it the changes are lost. If there anyway I can keep the changes. As I have mentioned before I am not sure if this is possible.

    thanks for your help.

Bookmarks

Posting Permissions

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