How to create editable div using JavaScript ?
Last Updated :
31 Jul, 2024
In this article, we will be explaining to you how to create an editable div using HTML, CSS, and JavaScript. An editable div is one on which is you will click then it will generate an editable text area to edit or to write any text on your browser itself. After editing, when you click on somewhere else on your browser then that text will be displayed as a constant text (without editable).
Prerequisites: You should be familiar with the basics of HTML, CSS, and JavaScript.
Creating structure: Create two files for HTML, and JavaScript. To create these files run the following command
Syntax:Â
$ touch index.html app.js
Step 1: Edit index.html. This file contains the following Code.
HTML
<!DOCTYPE html>
<html lang="en">
<body>
<h1 style="color: green;
text-align: center;">
Creating Editable Div GeeksforGeeks
</h1>
<div style="text-align: center;
margin-left: 35%;"
class="container">
<div id="first"></div>
</div>
</body>
<script src="app.js"></script>
</html>
Step 2: Edit the app.js file. This file contains the  following Code.
JavaScript
// Creating a new element
let editableDiv = document.createElement('div');
// Adding text to that created element
let value = localStorage.getItem('text');
let text;
if (value == null){
text = document.createTextNode
('Click on it to edit this Editable Div');
}
else{
text = document.createTextNode(value);
}
editableDiv.appendChild(text);
editableDiv.setAttribute('id', 'elem');
editableDiv.setAttribute('class', 'elem');
editableDiv.setAttribute('style',
'font-size:3rem;border:3px solid;
width:350px;height:200px;');
// Access the main container
let container = document.querySelector('.container');
// Inserting the element with id = first
container.insertBefore(editableDiv, first);
// Adding event listener to the divElem
editableDiv.addEventListener('click',function (){
let lengthOfTextAreas =
document.getElementsByClassName('textarea').length;
if(lengthOfTextAreas == 0){
let html = elem.innerHTML;
editableDiv.innerHTML =
`<textarea class="textarea form-control"
id="textarea" rows="3">
${html}</textarea>`;
}
// Listening the blur event on textarea
let textarea = document.getElementById('textarea');
textarea.addEventListener('blur',function() {
elem.innerHTML = textarea.value;
localStorage.setItem(
'text', textarea.value);
})
});
Final Solution: This is the combination of the above two steps.
HTML
<!DOCTYPE html>
<html lang="en">
<body>
<h1 style="color: green;
text-align: center;">
Creating Editable Div GeeksforGeeks
</h1>
<div style="text-align: center;
margin-left: 35%;"
class="container">
<div id="first"></div>
</div>
</body>
<script>
// Creating a new element
let editableDiv = document.createElement('div');
// Adding text to that created element
let value = localStorage.getItem('text');
let text;
if (value == null){
text = document.createTextNode
('Click on it to edit this Editable Div');
}
else{
text = document.createTextNode(value);
}
editableDiv.appendChild(text);
editableDiv.setAttribute('id', 'elem');
editableDiv.setAttribute('class', 'elem');
editableDiv.setAttribute('style',
'font-size:3rem;border:3px solid;
width:350px;height:200px;');
// Access the main container
let container = document.querySelector('.container');
// Inserting the element with id = first
container.insertBefore(editableDiv, first);
// Adding event listener to the divElem
editableDiv.addEventListener('click',function (){
let lengthOfTextAreas =
document.getElementsByClassName('textarea').length;
if(lengthOfTextAreas == 0){
let html = elem.innerHTML;
editableDiv.innerHTML =
`<textarea class="textarea form-control"
id="textarea" rows="3">
${html}</textarea>`;
}
// Listening the blur event on textarea
let textarea = document.getElementById('textarea');
textarea.addEventListener('blur',function() {
elem.innerHTML = textarea.value;
localStorage.setItem(
'text', textarea.value);
})
});
</script>
</html>
Output: When you will open it on any browser then the following output would be generated.
Similar Reads
How to Create an Image Element using JavaScript? Creating an image element dynamically using JavaScript is a useful technique when you want to add images to a webpage without having to manually write the <img> tag in your HTML. This allows for more flexibility, as we can create and manipulate images based on user interactions or other condit
2 min read
How to create a FAQ page using JavaScript ? The frequently Asked Questions (FAQ) section is one of the most important sections of any website, especially if you are providing services. If you want to learn how to make it by yourself then welcome! today we'll learn how to create a FAQ page using JavaScript. Functionalities required in a FAQ pa
6 min read
How to append data to <div> element using JavaScript ? To append the data to <div> element we have to use DOM(Document Object Model) manipulation techniques. The approach is to create a empty <div> with an id inside the HTML skeleton. Then that id will be used to fetch that <div> and then we will manipulate the inner text of that div.
1 min read
How to append HTML code to a div using JavaScript ? The ability to append HTML code to a <div> Using JavaScript helps enhance user experience by enabling dynamic content updates without reloading the page. This technique is widely usedâover 85% of modern websites implement JavaScript DOM manipulation to create smoother and more interactive web
6 min read
How to add HTML elements dynamically using JavaScript ? We learn how to add HTML elements dynamically using JavaScript. A basic understanding of HTML, CSS, and JavaScript is required. Here we are going to use a button and by clicking this button, we can add an HTML element dynamically in this example. Below are the approaches used to add HTML elements dy
2 min read
Create a Guestbook using HTML CSS and JavaScript This article will show how to create a GuestBook using HTML, CSS, and JavaScript that allows users to add their name, mobile number, room number, and address to the guestbook. We have written a code that generates a card with multiple input boxes and a submit button to store the guest's information
3 min read
How to create Edit icon using jQuery Mobile ? jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be making Edit icon using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ href=
1 min read
How to Set Cursor Position in Content-Editable Element using JavaScript? To set the cursor position in content-editable elements, such as a div tag, you can use the JavaScript Range interface. A range is created using the document.createRange() method.Approach:First, create a Range and set the position using the above syntax.Get user input from the input tag using jQuery
3 min read
How to dynamically create new elements in JavaScript ? New elements can be dynamically created in JavaScript with the help of createElement() method. The attributes of the created element can be set using the setAttribute() method. The examples given below would demonstrate this approach. Example 1: In this example, a newly created element is added as a
4 min read
How to make Live Coding Editor using HTML CSS and JavaScript ? In this article, we will implement a live coding editor using HTML, CSS, and JavaScript. This project will allow you to write and execute HTML, CSS, and JavaScript code in real-time, making it an excellent tool for learning and testing your code snippets.Final OutputPrerequisiteHTMLCSSJavaScriptAppr
3 min read