CoderZone.org

Category: >> JavaScript >> AJAX Inline Editor #1 Bookmark and Share

<< lastnext >>

Snippet Name: AJAX Inline Editor #1

Description: Edit-in-place with this slick bit of code.

Comment: (none)

Author: CoderZone
Language: JAVASCRIPT
Highlight Mode: JAVASCRIPT
Last Modified: February 27th, 2009

<html>
<head>
<script>
var timerlen = 5;
var slideAniLen = 250;
 
var timerID = new Array();
var startTime = new Array();
var obj = new Array();
var endHeight = new Array();
var moving = new Array();
var dir = new Array();
 
function slidedown(objname){
        if(moving[objname])
                return;
 
        if(document.getElementById(objname).style.display != "none")
                return; // cannot slide down something that is already visible
 
        moving[objname] = true;
        dir[objname] = "down";
        startslide(objname);
}
 
function slideup(objname){
        if(moving[objname])
                return;
 
        if(document.getElementById(objname).style.display == "none")
                return; // cannot slide up something that is already hidden
 
        moving[objname] = true;
        dir[objname] = "up";
        startslide(objname);
}
 
function startslide(objname){
        obj[objname] = document.getElementById(objname);
 
        endHeight[objname] = parseInt(obj[objname].style.height);
        startTime[objname] = (new Date()).getTime();
 
        if(dir[objname] == "down"){
                obj[objname].style.height = "1px";
        }
 
        obj[objname].style.display = "block";
 
        timerID[objname] = setInterval('slidetick(\'' + objname + '\');',timerlen);
}
 
function slidetick(objname){
        var elapsed = (new Date()).getTime() - startTime[objname];
 
        if (elapsed > slideAniLen)
                endSlide(objname)
        else {
                var d =Math.round(elapsed / slideAniLen * endHeight[objname]);
                if(dir[objname] == "up")
                        d = endHeight[objname] - d;
 
                obj[objname].style.height = d + "px";
        }
 
        return;
}
 
function endSlide(objname){
        clearInterval(timerID[objname]);
 
        if(dir[objname] == "up")
                obj[objname].style.display = "none";
 
        obj[objname].style.height = endHeight[objname] + "px";
        delete(moving[objname]);
        delete(timerID[objname]);
        delete(startTime[objname]);
        delete(endHeight[objname]);
        delete(obj[objname]);
        delete(dir[objname]);
 
        return;
}
 
 
// single link slide-up/slide-down function
function slide(objname){
    if(document.getElementById(objname).style.display == "none"){
        slidedown(objname);
    }else{
        slideup(objname);
    }
}
 
 
/* My functions added here */
function edit_it()
{
 var store = document.getElementById("view_it").innerHTML;
 document.getElementById("view_it").style.display="none";
 document.getElementById("edit_it").style.display="block";
 document.getElementById("t1").innerHTML=store;
 document.getElementById("mydiv").style.background="#cccccc";
 }
 
 
function save_it()
{
 //var store = document.getElementById("t1").innerHTML;
var modtext = document.xx.t1.value;
 document.getElementById("edit_it").style.display="none";
 document.getElementById("view_it").style.display="block";
 //document.getElementById("view_it").innerHTML=store;
document.getElementById("view_it").innerHTML=modtext;
 document.getElementById("mydiv").style.background="#FCF9D7";
 
alert(modtext);
 
 
 }
 
 
 
function cancel_it()
{
 document.getElementById("edit_it").style.display="none";
 document.getElementById("view_it").style.display="block";
 document.getElementById("mydiv").style.background="#FCF9D7";
 }
/*End of my functions*/
</script>
<style type="text/css">
.red{color:black;background:#FCF9D7;font-family:times new roman;padding:10px;
cursor: pointer;
}
.edit_edit {
background:#FFF;
margin:0px;
padding:2px;
color:#000;
}
.editButtons {
    background:gray;
    float:left;
    padding:0px 4px;
    margin:2px;
    color:#FFF ! important;
    text-Decoration:none;    
    font-Size:.8em;
    font-family:Arial, Helvetica, sans-serif;
    font-weight: bold;  
     font-size: 11px;   
}
.editButtons:hover {
    background:#000;
    color:#FFF;
}
</style>
</head>
<body>
<hr>
 
<a href="javascript:;" onmousedown="slide('mydiv');">Show/Hide</a> 
<div id="mydiv" style="display:block; border-style: solid; border-width: 1px; background-color: #ffffdd; overflow:hidden;width:440px;height:205px;padding:2px;">
 <div  id="view_it" class="red" style="width:400px;display:block;" onclick="edit_it();">
   Phasellus sem purus, Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
   Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec gravida. Etiam dignissim.
   Quisque ultricies. In hac habitasse platea dictumst. Pellentesque nec ligula.
   Quisque ultricies. In hac habitasse platea dictumst. Donec gravida. Etiam dignissim.
   Pellentesque nec ligula. Phasellus sem purus,
   convallis non, ornare ut, iaculis eu, eros. .
 </div>
 <div id="edit_it" style="width:400px;display:none;">
<form name="xx">
  <textarea id="t1" style="width:400px;height:155px;">
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 26