Simple implementation of double-click div into editable Area

Source: Internet
Author: User

Double-click the input element to replace the div with a newly created input element. When the input loses focus, the content of the input is first transmitted to the original div, and the input is replaced with the div. The Code is as follows:

 

[Javascript]
Window. onload = function (){
// It is initialized during loading. this indicates the div whose id is oldDiv.
Document. getElementById ("divElement"). ondblclick = function (){
ToReplace (this)
}
}
 
// This function is used to create an input element to replace the div.
// When the input element loses focus, it changes back to the original div
ToReplace = function (divElement ){
// Create an input element
Var inputElement = document. createElement ("input ");
// Assign the elements and text content in obj to the newly created inputElement.
InputElement. value = divElement. innerHTML;
 
// Replace the original oldDivElement element with the newly created inputElement
Document. body. replaceChild (inputElement, divElement );
// Trigger the following function when inputElement loses focus so that the input is converted into a div
InputElement. onblur = function (){
// Hand over the input value to the original div
DivElement. innerHTML = inputElement. value;
// Replace inputElement with the original div
Document. body. replaceChild (divElement, inputElement );
}
}
 

<Div id = "divElement"> double-click the text to edit the text </div>

Note: When you want to control a div to implement this function, you can double-click the event ondblclick.

For example, <div id = "someDiv" ondblclick = "toReplace (this)"> double-click the text to implement editable state </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.