SelectionJS is a lightweight JavaScript library for handling text selection and modifications. It works with contentEditable elements, input fields, and textarea, allowing easy text manipulation, event handling, and middleware-based transformations.
✅ Select and modify text in contentEditable, input, and textarea elements
✅ Supports appending, prepending, replacing, and wrapping text
✅ Event listeners to capture selection and modification events
✅ Middleware support to process text before modification
✅ Supports multiple containers for text selection
✅ Debounced selection processing for optimized performance
<script src="../dist/selection.min.js"></script><script src="https://cdn.jsdelivr.net/gh/deepansumor/SelectionJS@latest/dist/selection.min.js"></script>// Initialize SelectionJS
SelectionJS.configure({
container: document.querySelector(".editable-container"),
wrapperPlaceholder: "$"
});
// Listen for selection
SelectionJS.on("select", ({ text, modify }) => {
console.log("Selected Text:", text);
modify({ action: "wrap", text: "<b>$</b>" });
});SelectionJS.on("select", ({ text, modify }) => {
modify({ action: "prepend", text: "[PRE] " });
});Configures the library.
SelectionJS.configure({
container: [document.getElementById("editor1"), document.getElementById("editor2")],
wrapperPlaceholder: "$"
});Registers an event listener.
SelectionJS.on("update", ({ text }) => {
console.log("Updated text:", text);
});Modifies selected text.
replace- Replaces selection with new textappend- Appends text after selectionprepend- Adds text before selectionwrap- Wraps selection with provided text
Example:
modify({ action: "wrap", text: "<i>$</i>" });- Fork the repo
- Create a new branch (
git checkout -b feature-name) - Commit changes (
git commit -m "Added new feature") - Push to branch (
git push origin feature-name) - Open a Pull Request
MIT License