AJAX Edit in Place implemented by Prototype

Source: Internet
Author: User

Prototype seems to have been exhausted, but many people are using it. This code is used to implement the Edit In Place function.

// Requires prototype. js
Function edit (action, obj ){
Element. hide (obj );
Var textarea = '<div id = "' + obj. id + '_ editor "> <input type =" text "id ="' + obj. id + '_ edit "name ="' + obj. id + '"value ="' + obj. innerHTML + '"size =" 40 "> ';
Var button = '<input id = "' + obj. id + '_ save "type =" button "value =" SAVE "/> <input id ="' + obj. id + '_ cancel "type =" button "value =" CANCEL "/> </div> ';
New Insertion. After (obj, textarea + button );
Event. observe (obj. id + '_ save', 'click', function () {saveChanges (action, obj)}, false );
Event. observe (obj. id + '_ cancel', 'click', function () {cleanUp (obj)}, false );
$ (Obj. id + "_ edit"). focus ();
$ (Obj. id + "_ edit"). select ();
}

Function cleanUp (obj, keepEditable ){
Element. remove (obj. id + '_ editor ');
Element. show (obj );
If (! KeepEditable) showAsEditable (obj, true );
}

Function saveChanges (action, obj ){
Var new_content = escape ($ F (obj. id + '_ edit '));

Obj. innerHTML = "Saving ...";
CleanUp (obj, true );

Var success = function (t) {editComplete (t, obj );}
Var failure = function (t) {editFailed (t, obj );}

Var url = 'Poll-ajax. php? A = '+ action;
Var pars = 'id = '+ obj. id +' & content = '+ new_content;
Var myAjax = new Ajax. Request (url, {method: 'post', postBody: pars, onSuccess: success, onFailure: failure });
}

Function editComplete (t, obj ){
Obj. innerHTML = t. responseText;
ShowAsEditable (obj, true );
}

Function editFailed (t, obj ){
Obj. innerHTML = 'Sorry, the update failed .';
CleanUp (obj );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.