jquery implements an editable div

Source: Internet
Author: User


The HTML is roughly as follows:

<ol id= "Ol_group" class= "List-group list_of_items" >    <li class= "List-group-item Completed_item" >        <div class= "Text_holder" > How is it            ?            <div class= "Btn-group pull-right" >                <button class= "Delete btn btn-warning" >Delete</button>                <button class= "edit btn btn-success" >Edit</button>            </div>        </div>        <div class= "checkbox" >            <label>                <input type= "checkbox" class= "Pull-right" >            </label>        </div>    </li></ol>
(This is only part of it, but the demo is enough)


Add an event to the Eidt button in the JS file and implement the. Test-holder Div's editable:

Body.on (' click ', '. Text_holder. Btn-group. Edit ', function () {var Divedit = $ (this). Parent (). parent ();    if (!divedit) {return;    } if (Divedit.children ("input"). Length > 0) {return;    } var mtext = Divedit.text (). substring (0, Divedit.text (). length-10);  var inputins = $ ("<input type= ' text '/>");        Create input box var oldtext = divedit.html (); Save the original value Inputins.width (Divedit.width ()/3*2);    Set input to div width consistent inputins.val (mtext); Divedit.html (""); Delete the original cell div content inputins.appendto (divedit). focus (). Select ();    Insert the input box code that you want to insert into the DOM node Inputins.click (function () {return false;    });        Handle carriage return and ESC event Inputins.keyup (function (event) {var keycode = Event.which;            if (keycode = =) {var NewText = Oldtext.replace (Mtext, $ (this). Val ());         Divedit.html (NewText);         Set new Value} if (keycode = =) {divedit.html (OldText); return old Value}}). blur (function (event) {            if ($ (this). val ()! = OldText) {var newtext = Oldtext.replace (Mtext, $ (this). Val ());         Divedit.html (NewText);            Set new value}else{divedit.html (OldText); }        }    );});



Resources:

http://blog.csdn.net/billhepeng/article/details/7409125



Copyright NOTICE: This article for Bo Master original article, welcome reprint, reproduced please indicate the source.

jquery implements an editable div

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.