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] " });
});
SelectionJS.configure(options: SelectionOptions)
Configures the library.
SelectionJS.configure({
container: [document.getElementById("editor1"), document.getElementById("editor2")],
wrapperPlaceholder: "$"
});
SelectionJS.on(event: SelectionEvent, callback: SelectionCallback)
Registers an event listener.
SelectionJS.on("update", ({ text }) => {
console.log("Updated text:", text);
});
modify({ action, text })
Modifies selected text.
replace
- Replaces selection with new textappend
- Appends text after selectionprepend
- Adds text before selectionwrap
- Wraps selection with provided textExample:
modify({ action: "wrap", text: "<i>$</i>" });
git checkout -b feature-name
)git commit -m "Added new feature"
)git push origin feature-name
)MIT License