ArticleZip > Contenteditable Allowing Only Plain Text Duplicate

Contenteditable Allowing Only Plain Text Duplicate

Have you ever come across a situation where you are working on a web project and you need to allow users to input text but want to restrict them from copying and pasting formatted content? This can be a common need when working on applications where plain text input is required. One way to achieve this functionality is by using the contenteditable attribute in HTML along with a bit of JavaScript magic. In this article, we will walk you through how to implement a contentEditable element that only allows plain text duplication.

### Understanding contentEditable Attribute
The contenteditable attribute in HTML5 allows you to make any element editable by the user. By setting the value to "true," you enable the element to be edited directly on the web page. This feature is handy when creating rich text editors or any application that requires user input.

### Implementing Plain Text Only
To restrict users from pasting formatted content into a contentEditable element, we can use JavaScript to intercept and remove any HTML formatting from the copied text. The key is to listen for the `paste` event and handle the incoming content accordingly.

Html

<div id="editable">Start typing here...</div>


const editable = document.getElementById('editable');

editable.addEventListener('paste', (event) =&gt; {
  event.preventDefault();
  
  const text = (event.originalEvent || event).clipboardData.getData('text/plain');
  
  document.execCommand('insertText', false, text);
});

In the code snippet above, we first define a div element with the id `editable` and make it editable by setting `contenteditable` to true. We then add an event listener to the `paste` event which is triggered when a user pastes content into the element. Within the event handler, we prevent the default paste behavior, extract the plain text from the clipboard data, and insert it into the editable element using `document.execCommand('insertText', false, text)`.

### Testing the Implementation
To test this implementation, simply copy formatted text from any source (e.g., a webpage, Word document) and paste it into the contentEditable element. You will notice that only plain text is pasted, stripping away any formatting such as styles, links, or images.

### Customizing the Behavior
You can further customize this implementation based on your requirements. For instance, you may want to allow certain formatting options while still restricting others. By modifying the JavaScript code, you can define rules for handling different types of content pasted by the user.

### Conclusion
In this article, we have explored how to implement a contentEditable element that allows only plain text duplication. By combining the contenteditable attribute in HTML with JavaScript event handling, you can create a user-friendly input mechanism that enforces plain text input, perfect for scenarios where formatting needs to be controlled. Give it a try in your next project and see how it enhances the user experience!

×