/**
*    @Author: Arash Karimzadeh
*    @Email:
*    @Desciption: Here, I post some of my recent
*    researches. Also you can see my code projects.
*/  
Ajax C # Chrome CMS dateNet Design Patterns includeMany JavaScript jBind Joomla jQuery Plugin rails ror RubyOnRails SQLite xul

+ All tags

Content View Hits : 337022
Bookmark and Share
Editable (jQuery Editable Plugin) PDF Print
Written by Arash Karimzadeh   
Friday, 09 January 2009 06:48

This is a real customizable jQuery editable plugin. Currently it can convert any tag (span, div, p and ...) to text input, password, textarea, drop-down list.
You can easily extend it by adding your own input type using its editableFactory object.


              
  1. $('div.editable').editable()

              
 

              
            

Above code will convert all div tags, which has .editable class, to editable.

click me!!!

Download Editable (jQuery In Place Editor)

Current version is 1.3.3
You can download it here.

jQuery Editable Plugin

  1. Formats
  2. Options
  3. Examples
  4. Extending Editable

Formats

  • $(selector).editable(options)
    For creating editable object.
  • $(selector).editable('disable') added in editable 1.3.0
    For disabling editable object after it's creation.
  • $(selector).editable('enable') added in editable 1.3.0
    For enabling a disabled editable object.
  • $(selector).editable('destroy') added in editable 1.3.0
    For destroying Editability of an editable object.

Options

  • type [string]

[default = 'text']
Currently the acceptable values are 'text','password',textarea','select'

You can extend these types by extending editableFactory which we will discuss later.
  • editBy [string] added in Editable 1.3.3

[default = 'click']
You can define jQuery event type : 'blur','dblclick','change','click'

This option may make major problem if you don't have enough knowledge about events order in different browsers.
  • submitBy [string]

[default = 'blur']
You can define jQuery event type : 'blur','dblclick','change','click'

If you set submit option, you cant use submitBy option.
  • submit [string]

Editables are by default submitted on double click. If you want to enable the submit button you must set a value for submit option.
For example set it to 'save'.

If you set type option to 'select' you'd better to define the submit option or set submitBy to blur or change.
  • cancel [string]

If you want to enable the cancel button you must set value such as 'discard' or whatever you want.

  • editClass [string:cssClass]

You can change the CSS class of your input element to the defined CSS class in your style sheet.

This style will affect just the input element. You can change submit button or cancel button styles by setting their styles in your CSS. For example if you are going to convert a div to editable you can assign a CSS class such as .editable and also define your style for buttons '.editable button'.
  • options [options]

It is used for passing customized data.

For example in using 'select' type you must define the options in key/value format:
{'key1':'value1','key2':'value2','key3':'value3',...}

You can also use it while you are extending it to use your own input tag.

  • onEdit [Function]

This function will be called each time you click on the text to convert it to edit status.After the tags converted to input tags this function will be fired.
You can use it to show a message or do some checking and block the inputs.


              
  1. function onEdit(content){
  2. this; //this is the current jQuery object
  3. alert(content.current+':'+content.previous)
  4. }

              
 

              
            

  • onSubmit [Function]

This will be called after user submit the value. Here you can check if value is change so send the new value to server.


              
  1. function onSubmit(content){
  2. this; //this is the current jQuery object
  3. alert(content.current+':'+content.previous)
  4. }

              
 

              
            

  • onCancel [Function] added in Editable 1.3.3

This will be called if you assign cancel option. Here you have access to current and previous value of editable object.


              
  1. function onCancel(content){
  2. this; //this is the current jQuery object
  3. alert(content.current+':'+content.previous)
  4. }

              
 

              
            

  • hidden Properties

You can access to current value or previous value of the editables any time by using $('editable selector').data('editable.current') or $('editable selector').data('editable.previous'). Be careful while you are using these two hidden properties.
You usually do not need these because you can easily access them in your onSubmit function and onEdit function by using content.current and content.previous.

Examples (Create Editable)

  1. This is an editable in simplest way we just add a onEdit function to it
    
                      
    1. $('.editable1').editable({onEdit:begin});
    
                      
     
    
                      
                    
    
                      
    1. function begin(){
    2. this.append('Click somewhere else to submit');
    3. }
    
                      
     
    
                      
                    

    *In these example don't forget to add jQuery ready function to each code. so your code must be like this

    
                      
    1. $(function(){
    2. $('.editable1').editable({onEdit:begin});
    3. function begin(){
    4. this.append('Click somewhere else to submit');
    5. }
    6. });
    
                      
     
    
                      
                    

    Click to edit me!
  2. Editable will return a jquery object so you can re-use it as you see below.
    
                      
    1. $('.editable2').editable({type:'textarea',submitBy:'dblclick'}).css('background-color','#39c');
    
                      
     
    
                      
                    
    Click to edit me! I will submit by dblclick!!!
  3. You can add checking after submiting or compare previous value with new value before calling Ajax function
    
                      
    1. $('.editable3').editable({
    2. type:'password',
    3. submit:'save',
    4. onSubmit:end
    5. });
    
                      
     
    
                      
                    
    
                      
    1. class="editable3">Enter Password

    
                      
     
    
                      
                    
    
                      
    1. function end(content){
    2. alert(content.current+':'+content.previous)
    3. }
    
                      
     
    
                      
                    

    Enter Password!

  4. When you are using 'select' type you must set the text of div or p or whatever tag you are using to one of the options values otherwise the value will not be selected by default.
    
                      
    1. $('.editable4').editable({
    2. type:'select',
    3. options:{'value1':'Item 1', 2:'Item 2', 'Item 3':'Item 3'},
    4. submit:'save',
    5. cancel:'cancel',
    6. editClass:'resultItem',
    7. onSubmit:end
    8. });
    
                      
     
    
                      
                    
    
                      
    1. class="editable4">Item 2

                  
 

                  
                
                
Item 2
As you see when the text change to drop-down it Item 2 is selected by default.

                  
  1. class="editable4">Item-2
  • <--! This is wrong because 'Item-2' is not defined in options -->
  • 
                      
     
    
                      
                    
                  
                
                

    Extending Editable

    You can easily extend jQuery Editable by reading this article.

    Tags: JavaScript | jQuery | Plugin

    Last Updated on Monday, 27 September 2010 12:46