JSFiddle - React, Tailwind, and code Playground

by Trần Ngọc Minh

HTML

<p id="demo"> </p>

JavaScript

var walk_the_DOM = function walk(node, func) {

    func(node);

    node = node.firstChild;

    while (node) {

        walk(node, func);

        node = node.nextSibling;

    }

};

var getElementsByAttribute = function (att, value) {

    var results = [];

    walk_the_DOM(document.body, function (node) {

        var actual = node.nodeType === 1 && node.getAttribute(att);

        if (typeof actual === 'string' &&

                (actual === value || typeof value !== 'string')) {

            results.push(node);

        }

    });

    return results;

};
alert(getElementsByAttribute('id','demo'));