Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How Do You Make A Custom Insertion Point With a Contenteditable Div In HTML?
A contenteditable div allows users to edit content directly in the browser. Creating a custom insertion point means programmatically placing the cursor at a specific position within the editable content using JavaScript's Range and Selection APIs.
The contenteditable Attribute
The contenteditable global attribute specifies whether an element should be user editable. When set to true, the browser enables editing functionality for that element, allowing users to modify its content directly.
Syntax
Following is the syntax for the contenteditable attribute
<element>Content</element>
To create a custom insertion point, we use JavaScript's Range and Selection objects. The createRange() method creates a range, setStart() positions the cursor, and getSelection() manages the current selection.
Basic Custom Insertion Point
Creating a custom insertion point involves setting the cursor position at a specific text node and character offset within a contenteditable element.
Example
Following example demonstrates how to create a custom insertion point using JavaScript
<!DOCTYPE html>
<html>
<head>
<title>Custom Insertion Point</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px; background-color: #f0f8ff;">
<h2>Click the button to set cursor position</h2>
<div id="editable" style="border: 2px solid #4CAF50; padding: 15px; min-height: 100px; background-color: white;">
Welcome to TutorialsPoint. This is editable content where we can set custom cursor positions.
</div>
<button onclick="setInsertionPoint()" style="margin-top: 10px; padding: 8px 15px;">Set Cursor After "Welcome"</button>
<script>
function setInsertionPoint() {
var editableDiv = document.getElementById("editable");
var range = document.createRange();
var selection = window.getSelection();
// Position cursor after "Welcome " (8 characters)
range.setStart(editableDiv.firstChild, 8);
range.collapse(true);
selection.removeAllRanges();
selection.addRange(range);
editableDiv.focus();
}
</script>
</body>
</html>
Clicking the button positions the cursor immediately after the word "Welcome" in the editable div. The cursor becomes visible and ready for text input at that specific location.
Custom Insertion with Line Break Handling
This approach handles Enter key presses to insert line breaks instead of creating new paragraphs, maintaining better control over content structure.
Example
<!DOCTYPE html>
<html>
<head>
<title>Line Break Insertion Point</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px; background-color: #e8f4fd;">
<h2>Press Enter to insert line breaks</h2>
<div class="editable-content" style="border: 2px solid #2196F3; padding: 15px; min-height: 80px; background-color: white;">
Type here and press Enter
</div>
<div class="editable-content" style="border: 2px solid #2196F3; padding: 15px; min-height: 80px; background-color: white; margin-top: 10px;">
Another editable area
</div>
<script>
document.addEventListener('keydown', function(event) {
if (event.key === 'Enter' && event.target.contentEditable === 'true') {
document.execCommand('insertLineBreak');
event.preventDefault();
}
});
</script>
</body>
</html>
This example creates multiple editable areas where pressing Enter inserts line breaks instead of new paragraphs, giving more precise control over content formatting.
Advanced Insertion Point with Styling
This example demonstrates creating custom insertion points with styled contenteditable elements and controlled user selection behavior.
Example
<!DOCTYPE html>
<html>
<head>
<title>Advanced Custom Insertion</title>
<style>
.container {
margin: 20px;
-webkit-user-select: none;
user-select: none;
}
.editable-box {
display: inline-block;
background-color: #a8e6a3;
color: #2e7d32;
width: 200px;
padding: 10px;
margin: 5px;
border: 2px solid #4caf50;
border-radius: 5px;
}
.description {
margin-top: 15px;
padding: 10px;
background-color: #fff3e0;
border-left: 4px solid #ff9800;
}
</style>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px; background-color: #f5f5f5;">
<h2>Custom Styled Editable Elements</h2>
<div class="container">
<div class="editable-box">
Edit this text
</div>
<div class="editable-box">
Modify content here
</div>
</div>
<div class="description">
Click on the green boxes above to edit their content. The surrounding container prevents text selection, focusing interaction on the editable elements only.
</div>
<button onclick="focusFirst()" style="margin-top: 15px; padding: 8px 15px;">Focus First Box</button>
<script>
function focusFirst() {
var firstBox = document.querySelector('.editable-box');
var range = document.createRange();
var selection = window.getSelection();
range.selectNodeContents(firstBox);
range.collapse(false); // Position at end
selection.removeAllRanges();
selection.addRange(range);
firstBox.focus();
}
</script>
</body>
</html>
This example creates styled editable boxes with controlled selection behavior. The button demonstrates programmatically focusing the first editable element and positioning the cursor at the end of its content.
How Custom Insertion Points Work
The process involves four key JavaScript objects and methods
Range Object Created with
document.createRange(), represents a fragment of the document that can contain nodes and parts of text nodes.Selection Object Obtained with
window.getSelection(), represents the range of text selected by the user or the position of the caret.setStart() Method Sets the start point of the range to a specific node and character offset.
collapse() Method Collapses the range to a single point, creating an insertion point rather than a selection.
Key Methods and Properties
Understanding the essential methods for creating custom insertion points
| Method/Property | Purpose | Usage |
|---|---|---|
document.createRange() |
Creates a new Range object | Initial step to define cursor position |
range.setStart(node, offset) |
Sets the start position of the range | Specifies exact location within text node |
range.collapse(true) |
Collapses range to insertion point | Converts selection to cursor position |
window.getSelection() |
Gets current selection object | Manages user's current selection/cursor |
selection.addRange(range) |
Applies the range to selection | Activates the custom cursor position |
Conclusion
Creating custom insertion points in contenteditable divs requires using JavaScript's Range and Selection APIs to programmatically position the cursor. This technique provides precise control over where users can edit content, enhancing the user experience in rich text editing applications.
