JSFiddle - React, Tailwind, and code Playground

by adelura

HTML

<p id="foo">Hey OD-13456 kfdjk</p>

JavaScript

// var textnode = foo.firstChild;
// var replacementNode = textnode.splitText(1);

function splitAt(node, ...indexes) {
	let result = [];
	indexes = Array.from(indexes).reverse(); // 2, 1

	indexes.forEach(function (index) {
    	result.unshift(node.splitText(index));
    });
    
    result.unshift(node);
    
    return result;
}

function wrapIntoSpan(node) {
	let a = document.createElement('a');
    
    a.setAttribute('href', 'http://google.com');
    
    a.appendChild(node);
    
    return a;
}

let nodes = splitAt(foo.firstChild, 4, 12);
let a = wrapIntoSpan(nodes[1]);

foo.insertBefore(a, nodes[2]);