This Javascript text splitter has been created to allow easy and seo friendly text splitting (for animations) in two lines of code, it's including words, letters, spaces and special characters splitting.
Example — Repository — Questions
Download the file and include it in your HTML
<p class="text">This is my cool text!</p>
<script src="js/SplitTextJS.js"></script>
Or install and import it as a module
$ npm install split-text-js
First, import SplitTextJS :
import SplitTextJS from 'split-text-js';
Then, use SplitTextJS :
<p class="text">This is my cool text!</p>
// Select the DOM element wich contains the target text
const text = document.querySelector('.text');
// Split the text and store the result
const splittedText = new SplitTextJS(text);
// Now you can access your splitted text in the returned object
// Acces words :
const textWords = splittedText.words;
// Acces letters :
const textChars = splittedText.chars;
// Acces spaces :
const textSpaces = splittedText.spaces;
// Acces original text :
const textOriginal = splittedText.originalText;
If everything went well you should see this.