JSFiddle - React, Tailwind, and code Playground

by jeffsturgis

HTML

<div>
    <h1>head tag here</h1>
    <ul>
        <li class="findMe">item</li>
        <li class="findMe">item</li>
        <li class="findMeTo">item</li>
        <li>item</li>
        <li>item</li>
        <li>item</li>
    </ul>
    <p>
        outside of a span
        <span class="findMe">this is in a span</span>
    </p>
</div>

JavaScript

var lib = {
    
    getElementsByClassName: function(class){
        
        /* first we test to see if the native method
         * exists and we use it if it does
         */
        if(!document.getElementsByClassName){
        
        // get an array of all DOM nodes on the page
        var domNodes = document.getElementsByTagName( "*" );
        var i;
            
        // count the number of DOM nodes for iteration later
        var domLen = domNodes.length;
            
        // split the classes into an array for iteration
        var classArray = class.split(" ");
            
        // count the number of class names for iteration later
        var classLen = classArray.length;
        var results = [];

        // test if the node has a class
        var hasClass = function(elem){
            if(elem && (elem.className || elem.getAttribute("className"))){
                
                // if there is only one class then we don't need to loop just test the node
                if(classLen === 1){
                        
                    /* notice the space added to the
                     * class name this ensures we test the
                     * exact name and don't have partial matches
                     */
                       return (" " + (elem.className || elem.getAttribute("className")) + " ")
                                .indexOf( " " +  class + " " ) > -1;
                   }
                
                // if there is more than one class we need to loop the class array
                var i;
                for(i = 0; i <= classLen; i++){
                    if( (" " + (elem.className || elem.getAttribute("className")) + " ")
                        .indexOf(" " +classArray[i]+ " " ) > -1){
                        return true;
                    }
                }
            }
        };
        
        // helper function not needed
        var makeArray = function( array, results ) {
        array =...