JSFiddle - React, Tailwind, and code Playground

by karim79

HTML

<body>
    <h1>Hello, let us test this shit</h1>
    
    <p>This is a paragraph <span>inner span</span></p>
    
    
    
</body>

JavaScript

$.fn.glitch = function () {
   
    var children = $(this).find("*");
    var rand = Math.floor(Math.random() * (children.length - 0 + 1)) + 0;
    var theChosenOne = children.eq(rand);
    var theText = $.trim(theChosenOne.text());
    
    if(theText.indexOf(" ") !== -1) {
        var words = theText.split(" ");
        var wordCount = words.length;
        console.log("wordcount is " + wordCount);
        var wordIndex = Math.floor(Math.random() * (wordCount - 0 + 1)) + 0;
        var newText = theText.replace(words[wordIndex], "<span class='glitch'>" + words[wordIndex] + "magaga</span>"); 

        theChosenOne.html($(newText));
        
    }
};


$("body").glitch();